ASP.NET MVC 3使用身份验证
|
如何使用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 – 在AppHarbor上获取文件内容
- asp.net-mvc-3 – 剃刀中的部分的默认内容
- asp.net-mvc – IoC Castle Windsor – 没有为此对象定义的
- .net – 如何在ASP成员资格中使用LogOut
- asp.net – IIS URL重写 – 具有多个域
- asp.net – 一个明智的PasswordStrengthRegularExpression
- .net – 如何序列化邮件?
- asp.net-core – 我需要在ASP.NET Core for Cache中调用Add
- asp.net-mvc-3 – 在部署服务器中编辑ASP.NET MVC 3 resx文
- 如何在ASP.NET MVC 3中正确实施“确认密码”?
- asp.net – WebAPI不返回XML,即使有正确的Accept
- asp.net – 为什么GridView在回发后不会将标题行
- asp.net – Repeater中DropDownList的SelectedVa
- asp.net-mvc – 在asp.net MVC3中调用局部视图
- ASP.NET MVC3 RAZOR:文件上载给出文件计数为零
- asp.net – 如何更改F#Canopy UI测试脚本中的下拉
- asp.net-mvc – 什么是应用程序洞察遥测(未配置)
- asp.net-mvc – 401响应代码的json请求与ASP.NET
- asp.net-mvc – 缓存直到ASP.NET MVC和Entity Fr
- asp.net-mvc – MVC应用程序调试错误:viewstate
