首页 新闻 会员 周边

MVC5的身份认证User.Identity.Name获取值总是为空

0
悬赏园豆:10 [已关闭问题] 关闭于 2018-01-24 11:10

登录成功后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();
}

 

花开花落-2014的主页 花开花落-2014 | 初学一级 | 园豆:67
提问于:2016-12-21 10:55
< >
分享
所有回答(2)
0

应该是要配置的.不是光登录就完了.你找篇完整的文章看吧.

吴瑞祥 | 园豆:29449 (高人七级) | 2016-12-21 11:13

基于Claims身份认证好像不用配置,不是基于form验证

支持(0) 反对(0) 花开花落-2014 | 园豆:67 (初学一级) | 2016-12-21 12:08
0

不小心删除了Startup.Auth.cs,IdentityModels.cs等相关文件,恢复后就可以啦

花开花落-2014 | 园豆:67 (初学一级) | 2016-12-21 19:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册