本文介绍了Asp.net身份:User.Identity.GetUserId()总是空和User.Identity.IsAuthenticated常是假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

见下面我的code:

        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);
        }

的用户ID总是空和User.Identity.IsAuthenticated永远是假的。但是,我可以查看哪些需要验证的视图ClientDetails

The UserId is always null and User.Identity.IsAuthenticated is always false. But I can view the View "ClientDetails" which required Authentication

推荐答案

我假设你的例子是从你的AccountController.Login()方法,code。我有同样的问题,因为你,但发现用户对象将不会被填充,直到下一个请求。试试这个办法:

I assume your example is the code from your AccountController.Login() method. I had the same problem as you but discovered that the User object won't be populated until the next request. Try this approach:

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
}

这篇关于Asp.net身份:User.Identity.GetUserId()总是空和User.Identity.IsAuthenticated常是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:21