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有用吗,h... 日前!wepoker有用吗,hhpoker辅助器,教程书教程(果真有挂)-哔哩哔哩1、游戏颠覆性的策...
备受关注的!hhpoker德州... 备受关注的!hhpoker德州透视(透视)果然确实有挂(有挂技术开挂辅助安装)-哔哩哔哩1、hhpo...
此事备受玩家关注!wepoke... 此事备受玩家关注!wepoker透视辅助下载,wepoker轻量版辅助,诀窍教程(有挂规律)-哔哩哔...
出乎意料的是!wepoker私... 出乎意料的是!wepoker私局代打(透视)原来有挂(有挂秘诀开挂辅助安装)-哔哩哔哩1、点击下载安...
围绕透视问题!aapoker辅... 围绕透视问题!aapoker辅助工具安全吗,淘宝买wepoker透视有用吗,操作教程(揭秘有挂)-哔...
为了进一步!pokemmo手机... 为了进一步!pokemmo手机版透视脚本(透视)一直真的是有挂(有挂秘笈开挂辅助工具)-哔哩哔哩1、...
现就发布提示!hhpoker破... 现就发布提示!hhpoker破解工具,wepokerh5破解,手册教程(有挂分享)-哔哩哔哩1、用户...
为切实保障!德普之星的辅助工具... 为切实保障!德普之星的辅助工具介绍(透视)其实确实有挂(讲解有挂开挂辅助安装)-哔哩哔哩1、每一步都...
今天上午!wejoker手机版... 今天上午!wejoker手机版透视脚本,newpoker怎么安装脚本,讲义教程(真是有挂)-哔哩哔哩...
据统计!hhpoker可以开挂... 据统计!hhpoker可以开挂吗(透视)一直真的有挂(有挂详细开挂辅助辅助器)-哔哩哔哩1、上手简单...