【发布时间】:2017-12-28 22:14:01
【问题描述】:
谁能检查下面的代码,让我知道为什么我总是假的(User.Identity.IsAuthenticated)??。我在浏览器上正确获取了 cookie,并且 能够从 Claim 中获取价值,但“User.Identity.IsAuthenticated”始终为 false。
public async Task<IActionResult> Login(string phoneNumber, int otp, string returnUrl)
{
if (this.accountService.ValidateOTP(phoneNumber, otp))
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.MobilePhone, phoneNumber),
new Claim(ClaimTypes.Name, phoneNumber)
};
var userIdentity = new ClaimsIdentity();
userIdentity.AddClaims(claim);
ClaimsPrincipal userPrincipal = new ClaimsPrincipal(userIdentity);
await HttpContext.Authentication.SignOutAsync("MyCookieMiddlewareInstance");
await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", userPrincipal,
new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
IsPersistent = false,
AllowRefresh = false
});
if (string.IsNullOrWhiteSpace(returnUrl))
{
return RedirectToAction("Create", "Ad");
}
else
{
return Redirect(returnUrl);
}
}
return BadRequest();
}
【问题讨论】:
标签: asp.net-core asp.net-core-mvc