asp.net-mvc – Asp.net Identity:User.Identity.GetUserId()始终为空,User.Identity.IsAuthenticated总是为false

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – Asp.net Identity:User.Identity.GetUserId()始终为空,User.Identity.IsAuthenticated总是为false前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
请参阅我的代码
var result = await SignInManager.PasswordSignInAsync(model.UserName,model.Password,model.RememberMe,shouldLockout: false);
switch (result)
{
    case SignInStatus.Success:
        string UserId = User.Identity.GetUserId(); 
        return RedirectToAction("ClientDetails","Home");
    case SignInStatus.LockedOut:
        return View("Lockout");
    case SignInStatus.RequiresVerification:
        return RedirectToAction("SendCode","Account",new { ReturnUrl = returnUrl,RememberMe = model.RememberMe });
    case SignInStatus.Failure:
    default:
        ModelState.AddModelError("","Invalid login attempt.");
        return View(model);
}

UserId始终为空,User.Identity.IsAuthenticated始终为false.但是我可以查看需要身份验证的View ClientDetails.

解决方法

我假设你的例子是你的AccountController.Login()方法代码.我有与您相同的问题,但发现用户对象将不会被填充,直到下一个请求.尝试这种方法
case SignInStatus.Success:
    return RedirectToAction("DoWork","Account");


public async Task<ActionResult> DoWork()
{
    //this works
    string UserId = User.Identity.GetUserId();
    //return to View or Redirect again
}
原文链接:https://www.f2er.com/aspnet/250346.html

猜你在找的asp.Net相关文章