gpt4 book ai didi

asp.net-mvc - Asp.net Identity : User. Identity.GetUserId() 始终为 null,User.Identity.IsAuthenticated 始终为 false

转载 作者:行者123 更新时间:2023-12-02 03:27:57 28 4
gpt4 key购买 nike

请参阅下面的代码:

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 始终为 nullUser.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
}

关于asp.net-mvc - Asp.net Identity : User. Identity.GetUserId() 始终为 null,User.Identity.IsAuthenticated 始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25439275/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com