_userManager.GetUserAsync(User)返回null [英] _userManager.GetUserAsync(User) returns null
问题描述
我正尝试在登录后检查用户的电子邮件确认状态,然后统一引导他们.
基于以下两个线程:
我尝试过:
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
ClaimsPrincipal currentUser = this.User;
var thisUser = await _userManager.GetUserAsync(currentUser);
if(thisUser.EmailConfirmed)
{
return View("~/Views/Task/Index.cshtml");
}
else
{
return View("ConfirmEmail");
}
}
还有这个:
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
var thisUser = await _userManager.GetUserAsync(HttpContext.User);
if(thisUser.EmailConfirmed)
{
return View("~/Views/Task/Index.cshtml");
}
else
{
return View("ConfirmEmail");
}
}
从控制器内部开始,但thisUser
始终为空.
我如何在登录时检查其电子邮件已得到确认并正确重定向?
您的方法有一个问题,对于Membership
和Identity
来说是正确的:它们基于cookies
. Cookies只有在客户端发送的情况下才能被读取.
所以,这就是你的流程:
- 设置cookie
- 读取cookie
如上所述,这是错误的.您的流程应该是:
- 设置cookie
- 重定向到某个地方
- 读取cookie(现在由客户端发送)
OR
- 设置cookie
- 根据已有的
email
,从存储在任何位置的数据中读取数据
I am trying to check a users email confirmation status after login and then direct them accordigly.
Based on these two threads:
ASP.NET Core Identity - get current user
How get current user in asp.net core
I tried this:
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
ClaimsPrincipal currentUser = this.User;
var thisUser = await _userManager.GetUserAsync(currentUser);
if(thisUser.EmailConfirmed)
{
return View("~/Views/Task/Index.cshtml");
}
else
{
return View("ConfirmEmail");
}
}
And also this:
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
var thisUser = await _userManager.GetUserAsync(HttpContext.User);
if(thisUser.EmailConfirmed)
{
return View("~/Views/Task/Index.cshtml");
}
else
{
return View("ConfirmEmail");
}
}
From inside controller but thisUser
is always null.
How do I check on logon that their email is confirmed and re-direct appropriately?
There's a problem to your approach, which is true for Membership
and Identity
: they're based on cookies
. Cookies can only be read if they are sent by the client.
So, this is your flow:
- Set cookie
- Read cookie
This is wrong as explained above. Your flow should either be:
- Set cookie
- Redirect somewhere
- Read cookie (which now was sent by the client)
OR
- Set cookie
- Read data from wherever it's stored basing on the
email
you already have
这篇关于_userManager.GetUserAsync(User)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!