且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET Core身份-脚手架身份后LoginPartial损坏

更新时间:2023-02-25 22:33:08

存在 GitHub问题描述您遇到的确切问题.在脚手架之前间接使用的Razor类库(RCL)实现在Logout.cshtml.cs中具有OnPost实现,该实现类似于

There's a GitHub issue that describes the exact problem you're running in to. The Razor Class Library (RCL) implementation you were using indirectly before scaffolding has an OnPost implementation in Logout.cshtml.cs that looks like this:

public override async Task<IActionResult> OnPost(string returnUrl = null)
{
    await _signInManager.SignOutAsync();
    _logger.LogInformation("User logged out.");
    if (returnUrl != null)
    {
        return LocalRedirect(returnUrl);
    }
    else
    {
        // This needs to be a redirect so that the browser performs a new
        // request and the identity for the user gets updated.
        return RedirectToPage();
    }
}

正如内嵌注释所解释的,它是一个RedirectToPage,用于确保在清除身份后重新加载身份.如果您查看此方法的脚手架版本,则会发现它在else分支中具有以下内容:

As the inline comment explains, it's a RedirectToPage that's needed to ensure the identity gets reloaded after being cleared. If you look at your scaffolded version of this method, you'll find that it has the following in the else branch:

return Page();

就是这个问题.没有重定向,因此没有重新加载身份.您可以通过将其切换为使用RedirectToPage来解决该问题,如我在上面提到的RCL实现中所示.

That's the problem. There's no redirect, so there's no reloading of the identity. You can resolve the problem by switching that to use RedirectToPage, as shown in the RCL implementation I called out above.