如何使用AspNet.Identity以Asp.Net MVC5 RTM位login/validation用户?

道歉和感谢提前这个问题! 我还是新来的。

我一直在使用MVC5,EF6和VS 2013开发Web应用程序。

我花了一些时间升级到RC位,一旦释放。 感谢所有伟大的职位:例如。 解耦Microsoft.AspNet.Identity。*和更新asp.net MVC从5.0.0-beta2到5.0.0-rc1 !

以我的无限智慧,我决定转移到@Hao Kung在这里发布的RTM位: 我怎样才能尽早访问即将到来的Asp.Net Identity更改? 。 我想我会省下麻烦,并且不会太落后于我们最终收到RTM版本。

这或者是一场噩梦,或者我完全错过了一些(或者两者兼有),因为我无法弄清楚一直在使用RC1的基本任务。

虽然好像我通过控制器( Asp.Net身份RTM版本中的Microsoft.AspNet.Identity.Owin.AuthenticationManager? )在日志logging用户…我的WindowsIdentity总是空的,没有authentication后,我打电话SignIn。 用户和claimsIdentity对象正确填充。

下面是我调用的操作方法(为了完整性,将属性移动到局部variables):

[HttpPost, AllowAnonymous, ValidateAntiForgeryToken] public virtual async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var userManager = new UserManager<EtdsUser>(new UserStore<EtdsUser>(new EtdsContext())); var user = userManager.Find(model.UserName, model.Password); if (user != null) { var authenticationManager = HttpContext.GetOwinContext().Authentication; authenticationManager.SignOut(new[] {DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.ExternalBearer}); var claimsIdentity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe}, claimsIdentity); return RedirectToLocal(returnUrl); } } ModelState.AddModelError("", "The user name or password provided is incorrect."); return View(model); } 

(请注意:此时我不需要login外部用户。)

有什么build议么? – 或者 – 我应该回滚所有我的更改,只是等到VS 2013 RTMd?


更新,重构代码,使其更接近@Hao Kung的原始答复。 但是,我仍然没有得到一个有效的用户身份。 我认为我的AuthenticationManager分配不正确?

AuthenticationManger现在定义为:

 public IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } } 

SignInAsync现在是一个单独的方法:

 private async Task SignInAsync(EtdsUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var claimsIdentity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent}, claimsIdentity); } 

在“SignOut”之后,debugging器显示:

 AuthenticationManager.User.Identity {System.Security.Principal.WindowsIdentity} [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity} AuthenticationType: "" IsAuthenticated: false Name: "" 

那么“主张身份”就是:

 claimsIdentity {System.Security.Claims.ClaimsIdentity} Actor: null AuthenticationType: "ApplicationCookie" BootstrapContext: null Claims: {System.Security.Claims.ClaimsIdentity.get_Claims} IsAuthenticated: true Label: null Name: "alon" NameClaimType: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" RoleClaimType: "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" 

“login”不会改变任何东西:

 AuthenticationManager.User.Identity {System.Security.Principal.WindowsIdentity} [System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity} AuthenticationType: "" IsAuthenticated: false Name: "" 

仍然没有身份validation,但似乎没有错误抛出。


正如@Hao Kung所回答的那样,将StartUp.Auth.cs改为:

 var authOptions = new CookieAuthenticationOptions { ExpireTimeSpan = TimeSpan.FromHours(4.0)}; app.UseCookieAuthentication(authOptions); 

至:

 var authOptions = new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), ExpireTimeSpan = TimeSpan.FromHours(4.0) }; ... 

所以这里是什么login将基本上在RTM(从ASPNET身份示例代码复制的代码 ):

  // // POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var user = await UserManager.FindAsync(model.UserName, model.Password); if (user != null) { await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("", "Invalid username or password."); } } // If we got this far, something failed, redisplay form return View(model); } private async Task SignInAsync(ApplicationUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); } 

编辑:你需要在您的Startup.Auth.cs中的以下更改:

  app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login") });