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以查找更多信息。

相关内容

热门资讯

透视美元局!中至江西源代码,w... 透视美元局!中至江西源代码,wepoker游戏的安装教程(辅助挂)详细开挂辅助器1、下载安装好wep...
今日焦点!奇迹手游辅助,阿拉游... >>您好:阿拉游戏中心辅助工具确实是有挂的,很多玩家在这款阿拉游戏中心辅助工具游戏中打牌都会发现很多...
透视线上!浙江宝宝游戏辅助器,... 透视线上!浙江宝宝游戏辅助器,hhpoker作弊码怎么用(辅助挂)详细开挂辅助下载 了解更多开挂安装...
攻略讲解!极速官方暗堡透视,对... 您好:这款极速官方暗堡透视游戏是可以开挂的,确实是有挂的,很多玩家在这款极速官方暗堡透视游戏中打牌都...
透视好友!pokermaste... 透视好友!pokermaster破解版,wepoker辅助真的假的(辅助挂)详细开挂辅助神器;无需打...
信息共享!传送屋app辅助脚本... 大家好,今天小编来为大家解答传送屋app辅助脚本下载这个问题咨询软件客服可以免费测试直接加微信(13...
透视玄学!wepoker手机助... 您好:这款红龙poker辅助平台游戏是可以开挂的,确实是有挂的,很多玩家在这款红龙poker辅助平台...
新手必备!四川家园辅助器,陕麻... 新手必备!四川家园辅助器,陕麻圈有辅助吗(有挂方法辅助插件);无需打开直接搜索打开薇:1367043...
必知教程!潮汕娱手机暗宝透视,... 必知教程!潮汕娱手机暗宝透视,反杀大厅辅助(证实有挂开挂辅助下载);无需打开直接搜索薇:136704...
透视苹果版!德普之星透视辅助软... 透视苹果版!德普之星透视辅助软件激活码,wepoker到底有没有透视(辅助挂)详细开挂辅助器 了解更...