如何使用AspNet.Identity在Asp.Net MVC5 RTM版本中登录/认证用户?
如何使用AspNet.Identity在Asp.Net MVC5 RTM版本中登录/认证用户?
我一直在使用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 Identity RTM版本中,Microsoft.AspNet.Identity.Owin.AuthenticationManager在哪里?),但我的WindowsIdentity始终为空且未经过身份验证,即使在调用SignIn后也是如此。用户和claimsIdentity对象的填充是正确的。
下面是我调用的操作方法(为了完整起见,将属性移至局部变量):
[HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
public virtual async Task
{
if (ModelState.IsValid) {
var userManager = new UserManager
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("", "用户名或密码不正确。");
return View(model);
}
(顺便说一句:目前我不需要登录外部用户。)
有什么建议吗?
-或者-
我应该撤销所有的改变,等到VS 2013发布吗?
更新,重构代码以使其更接近@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”之后,调试器显示:
AuthenticationManager.User.Identity
{System.Security.Principal.WindowsIdentity}
[System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
AuthenticationType: ""
IsAuthenticated: false
Name: ""
然后“claimsIdentity”是:
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"
“SignIn”没有改变任何内容:
AuthenticationManager.User.Identity
{System.Security.Principal.WindowsIdentity}
[System.Security.Principal.WindowsIdentity]: {System.Security.Principal.WindowsIdentity}
AuthenticationType: ""
IsAuthenticated: false
Name: ""
仍然没有身份验证,但似乎没有抛出任何错误。
根据@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)
}; ...