Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# _GetUserAsync(用户)返回null_C#_Asp.net Core_Asp.net Core Mvc_Asp.net Core 2.0 - Fatal编程技术网

C# _GetUserAsync(用户)返回null

C# _GetUserAsync(用户)返回null,c#,asp.net-core,asp.net-core-mvc,asp.net-core-2.0,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Core 2.0,我尝试在登录后检查用户的电子邮件确认状态,然后相应地指导他们 基于这两个线程: 我试过这个: var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false); if (result.Succeeded) { ClaimsPrincipal currentUser = this.User; v

我尝试在登录后检查用户的电子邮件确认状态,然后相应地指导他们

基于这两个线程:

我试过这个:

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");
    }
}
来自控制器内部,但此用户始终为空


我如何在登录时检查他们的电子邮件是否得到确认并正确重新定向?

您的方法有一个问题,这对于
成员身份和
身份是正确的:它们基于
cookies
。Cookie只有在客户端发送时才能读取

这就是你的流程:

  • 设置cookie
  • 阅读曲奇
  • 如上所述,这是错误的。您的流程应该是:

  • 设置cookie
  • 重定向到某个地方
  • 读取cookie(现在由客户端发送)
  • 设置cookie
  • 根据您已有的
    电子邮件从存储数据的任何位置读取数据

  • 我在模型中有电子邮件,所以我只是直接在数据库中查找它

    var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
    if (result.Succeeded)
    {
        _logger.LogInformation("User logged in.");
    
        var user = _context.Users.Single(x => x.Email == model.Email);
    
        if(user.EmailConfirmed)
        {
            return View("~/Views/Task/Index.cshtml");
        }
        else
        {
            return View("ConfirmEmail");
        }
    
    }
    
    从:

    通过调用来启用标识。使用身份验证 向请求管道添加身份验证

    因此,在
    Configure
    方法中,如果您忘记了设置

    app.UseAuthentication();
    
    您将得到相同的结果,没有任何错误