[英]ASP.NET Identity user null after login
更新了代碼以更好地解決此問題后,我現在遇到以下問題:
所提供的防偽令牌是針對與當前用戶不同的基於聲明的用戶。
這是更新的代碼:
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
if (result == SignInStatus.Success)
{
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var manager = new UserManager<ApplicationUser>(store);
var user = manager.FindById(User.Identity.GetUserId());
GenerateUserCookie(model, user, returnUrl);
}
switch (result)
{
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
default:
return View(model);
}
private ActionResult GenerateUserCookie(LoginViewModel model, ApplicationUser user, string returnUrl)
{
if (user == null)
{
ModelState.AddModelError("", "Someething went wrong; please try logging in again.");
return View(model);
}
var cookie = new HttpCookie("stman");
if (user.AgentId >= 1) cookie.Values.Add("aid", user.AgentId.ToString());
cookie.Values.Add("wumpus", user.Id.ToString());
Response.Cookies.Add(cookie);
return RedirectToLocal(returnUrl);
}
就像標題所說的那樣,登錄后,以下代碼返回的用戶值仍然為null。
要真正獲得用戶,我必須再次打開登錄屏幕(不刷新)並再次登錄...
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var manager = new UserManager<ApplicationUser>(store);
var user = manager.FindById(User.Identity.GetUserId());
GenerateUserCookie(user);
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
private void GenerateUserCookie(ApplicationUser user)
{
var cookie = new HttpCookie("stman");
// Throws exception most times since the user is null.
if (user.AgentId >= 1)
{
cookie.Values.Add("aid", user.AgentId.ToString());
}
cookie.Values.Add("wumpus", user.Id.ToString());
Response.Cookies.Add(cookie);
}
問題在於,直到您重定向后, User.Identity
用。 解決此問題的一種簡單方法是使用用戶名而不是id來獲取正確的ApplicationUser
。
var manager = new UserManager<ApplicationUser>(store);
var user = manager.FindByName(model.UserName);
GenerateUserCookie(user);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.