ASP.NET MVC 3使用身份验证
发布时间:2020-12-15 18:44:41 所属栏目:asp.Net 来源:网络整理
导读:如何使用FormsAuthentication保存内容?我不想通过URL存储UserId。 例如,现在我有这个代码: //UserController class:[HttpPost]public ActionResult LogOn(LogOnModel model,string returnUrl){if (ModelState.IsValid){ if (repository.ValidateUser(mode
如何使用FormsAuthentication保存内容?我不想通过URL存储UserId。
例如,现在我有这个代码: //UserController class: [HttpPost] public ActionResult LogOn(LogOnModel model,string returnUrl) { if (ModelState.IsValid) { if (repository.ValidateUser(model.Login,model.Password)) { FormsAuthentication.SetAuthCookie(model.Login,model.RememberMe); if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Project","Index"); } } else { ModelState.AddModelError("","Incorrect name or password."); } } return View(model); } ProjectController类: public ViewResult Index() { return View(repository.GetUserProjects( this.ControllerContext.HttpContext.User.Identity.Name)); } ProjectRepository: ProjectsContext context = new ProjectsContext(); UsersContext uCnt = new UsersContext(); public IEnumerable<Project> GetUserProjects(String username) { if (String.IsNullOrEmpty(username)) throw new ArgumentNullException("username","Login is empty"); return this.uCnt.Users .FirstOrDefault(u => u.Login == username) .Projects .ToList(); } ProjectController和ProjectRepository看起来不是很好的代码…也许有人可以提供建议,如何存储UserID而不使用URL?我认为最好的方法是保存自动识别ID。我没有在User.Identity中找到任何属性来执行此操作 UPD 我乞求赦免,但我忘了说我在使用MVC-3与剃须刀视图。 解决方法
当用户登录时,将UserID保存在授权cookie中的FormsAuthentication故障单的UserData属性中:
string userData = userID.ToString(); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,user.Email,DateTime.Now,DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),createPersistentCookie,userData); string hashedTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,hashedTicket); HttpContext.Current.Response.Cookies.Add(cookie); 您可以在Global.asax中的PostAuthenticateRequest方法中阅读: HttpCookie formsCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (formsCookie != null) { FormsAuthenticationTicket auth = FormsAuthentication.Decrypt(formsCookie.Value); Guid userID = new Guid(auth.UserData); var principal = new CustomPrincipal(Roles.Provider.Name,new GenericIdentity(auth.Name),userID); Context.User = Thread.CurrentPrincipal = principal; } 请注意,在这种情况下,CustomPrincipal派生自RolePrincipal(虽然如果您不使用Roles,我认为您需要从GenericPrincipal派生),并且只需添加UserID属性并重构构造函数。 现在,无论您在应用中需要UserID,您都可以这样做: if(HttpContext.Current.Request.IsAuthenticated) Guid userID = ((CustomPrincipal)HttpContext.Current.User).UserID; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 我的bin部署MVC4应用程序中的区域路由有什么
- asp.net-mvc – MVC捆绑客户端缓存
- 打造自己的.NET Core项目模板
- asp.net – 在string.format中添加单行if语句
- asp.net-mvc – 在web.config文件中创建自定义变量?
- asp.net-mvc – MVC Razor获取月份名称
- asp.net – 如何将转发器绑定到列表以更新绑定的项目? (2种
- asp.net-mvc – 初学者的分析Qn – ASP .NET MVC Mini-Prof
- asp.net – Visual Studio 2010调试速度慢
- 实体框架 – 具有实体框架的MVC MiniProfiler 5模型第一个错