【发布时间】:2018-07-11 05:40:20
【问题描述】:
我正在处理一个项目,需要为几个功能添加授权。目前,设置非常简单。我要做的就是登录并获得授权。但是,登录功能成功,而 User.Identity.IsAuthenticated 仍设置为 false,导致无法访问任何标记为 [Authorize] 的内容。
我应该如何解决这个问题?我是否应该检查 DBO.AspNetUserLogins 中的条目以查看登录是否已记录?
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel details, string returnUrl)
{
if (ModelState.IsValid)
{
AppUser user = await userManager.FindByEmailAsync(details.Email);
if (user != null)
{
await signInManager.SignOutAsync();
Microsoft.AspNetCore.Identity.SignInResult result = await signInManager.PasswordSignInAsync(user, details.Password, false, false);
if (result.Succeeded)
{
if (User.Identity.IsAuthenticated)
{
//Just curious if this is true.
}
return Redirect(returnUrl);
}
}
ModelState.AddModelError(nameof(LoginViewModel.Email), " Invalid user or password");
}
return View(details);
}
【问题讨论】:
标签: c# asp.net-mvc authentication asp.net-mvc-5