ASP.NETMVC中的登录验证和授权未能正常工作。
创始人
2024-09-19 08:31:15
0
  1. 确保您已启用ASP.NET身份验证。您可以通过在Web.config文件中的标记内设置mode属性为“Forms”来启用它。示例:

  1. 确保您对需要访问权限的控制器或动作使用了Authorize属性。例如:

[Authorize] public ActionResult SecureAction() { return View(); }

  1. 确保您在登录后设置了用户的身份验证Cookie。示例:

var ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), false, userData, "/"); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Response.Cookies.Add(authenticationCookie);

  1. 确保您的登录页面正确执行身份验证。示例:

public ActionResult Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var user = _userService.Authenticate(model.UserName, model.Password); if (user != null) { var userData = GetUserSpecificData(user); var ticket = new FormsAuthenticationTicket(1, user.UserName, DateTime.Now, DateTime.Now.AddMinutes(30), false, userData, FormsAuthentication.FormsCookiePath); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Response.Cookies.Add(authenticationCookie); if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "Invalid username or password."); } } return View(model); }

如果您的问题仍然存在,请确保在代码中使用Debug和Trace以查找更多信息。

相关内容

热门资讯

第一分钟模块!wepoker透... 第一分钟模块!wepoker透视底牌脚本(透视)切实真的是有辅助辅助(哔哩哔哩)一、wepoker透...
第9分钟绝活儿!红龙poker... 第9分钟绝活儿!红龙poker作必弊指令(透视)竟然存在有辅助工具(哔哩哔哩)一、红龙poker作必...
第3分钟办法!哈糖大菠萝破解器... 第3分钟办法!哈糖大菠萝破解器(透视)总是是有辅助教程(哔哩哔哩)1、进入到哈糖大菠萝破解器是否有挂...
四分钟步骤!wejoker辅助... 四分钟步骤!wejoker辅助软件视频(透视)确实存在有辅助神器(哔哩哔哩)进入游戏-大厅左侧-新手...
6分钟攻略!wejoker辅助... 6分钟攻略!wejoker辅助软件(透视)都是是真的有辅助透视(哔哩哔哩)wejoker辅助软件辅助...
第五分钟指南书!hhpoker... 第五分钟指南书!hhpoker免费透视脚本(透视)其实是真的有辅助教程(哔哩哔哩)1)hhpoker...
第九分钟绝活!wepoker有... 第九分钟绝活!wepoker有没有机器人(透视)一贯是有辅助教程(哔哩哔哩)1、wepoker有没有...
第9分钟讲义!wepoker透... 第9分钟讲义!wepoker透视底牌脚本(透视)总是真的是有辅助透视(哔哩哔哩)1、许多玩家不知道w...
四分钟大纲!epoker免费透... 四分钟大纲!epoker免费透视脚本(透视)竟然是真的有辅助下载(哔哩哔哩)1、任何epoker免费...
第二分钟指南!wepoker免... 第二分钟指南!wepoker免费透视脚本(透视)都是是有辅助透视(哔哩哔哩)1、许多玩家不知道wep...