登录成功后User.Identity.Name获取值总是为空,调试发现 User.Identity.IsAuthenticated为false,请大神指点,非常感谢
BLL创建声明代码:
public ClaimsIdentity CreateIdentity(User user, string authenticationType)
{
ClaimsIdentity _identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
_identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
_identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserID.ToString()));
_identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity"));
_identity.AddClaim(new Claim("DisplayName", user.DisplayName));
return _identity;
}
登录代码:
public ActionResult Login(LoginViewModel loginViewModel)
{
if (ModelState.IsValid)
{
var _user = userService.Find(loginViewModel.UserName);
if (_user == null) ModelState.AddModelError("UserName", "用户名不存在");
else if (_user.PassWord == Common.Security.Sha256(loginViewModel.Password))
{
_user.LoginTime = System.DateTime.Now;
_user.LoginIP = Request.UserHostAddress;
var _identity = userService.CreateIdentity(_user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = loginViewModel.RememberMe }, _identity);
return RedirectToAction("Index", "Home");
}
else ModelState.AddModelError("Password", "密码错误");
}
return View();
}
应该是要配置的.不是光登录就完了.你找篇完整的文章看吧.
基于Claims身份认证好像不用配置,不是基于form验证
不小心删除了Startup.Auth.cs,IdentityModels.cs等相关文件,恢复后就可以啦