为什么即使我注销了User.Identity.IsAuthenticated仍然设置为true

时间:2020-02-26 13:25:52

标签: c# asp.net-core asp.net-core-identity

我有一些遗留问题。

比方说,在同一浏览器上,我有一个已经登录的用户,另一个用户想确认他或她的电子邮件以创建他或她的密码,从而激活他或她的用户帐户。

事实证明,即使另一个用户确认他的电子邮件时,已经登录的用户也没有注销,尽管我调用了以下两种方法:

  • await SignInManager.SignOutAsync();
  • await UserManager.UpdateSecurityStampAsync(alreadyLoggedInUser);

控制器操作代码:

// GET: /Account/ConfirmEmail
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string code, string userId)
{
    if (code == null || userId == null)
    {
        return View("TokenError");
    }

    await SignInManager.SignOutAsync();

    var user = await UserManager.FindByIdAsync(userId);
    if (user == null)
    {
        Logger.LogInformation("User not found");
        return View("TokenError");
    }

    var alreadyLoggedInUser= await UserManager.GetUserAsync(User);
    await UserManager.UpdateSecurityStampAsync(alreadyLoggedInUser);

    HttpContext.Response.Cookies.Delete("Set-Cookie");

    if (await CheckAccountActivationTokenAsync(user, code))
    {
        return View("CreatePasswordView", new SetPasswordViewModel());
    }

    Logger.LogInformation("Invalid token");
    return View("TokenError");
}

返回的视图基于布局,并且在部分视图之一中,将进行检查以了解用户是否已通过身份验证,这是事实。

@using MySecretProject.Core.App_LocalResources
@{
    if (User.Identity.IsAuthenticated)
    {
        using (Html.BeginForm("LogOut", "Account", new { Area = "" }, FormMethod.Post, null, new { id = "logoutForm" }))
        {
            @Html.AntiForgeryToken()
            <a href="javascript:document.getElementById('logoutForm').submit()">@Resource.Logout</a>
        }
    }
}

我如何确保已登录的用户已正确注销,以便在为试图确认其电子邮件的用户创建密码的视图时,没有其他用户的踪迹。

1 个答案:

答案 0 :(得分:1)

尝试

HttpContext.SignOutAsync();

退出操作。