c# - ASP.NET Core Identity - LoginPartial 在脚手架身份后损坏

标签 c# asp.net-core razor-pages asp.net-core-2.2 asp.net-core-identity

我从 VS 2017 模板(具有个人用户帐户的 Web 应用程序)创建了一个新项目。

这会将 ASP.NET Core Identity 添加为默认 UI(使用 nuget 中的 UI)。

  services
    .AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

有了这个 nuget,一切都会按预期进行。特别是登录部分,它在用户登录后显示用户名,并在单击注销后立即显示登录按钮。

一旦我搭建了布局并应用了更改(根据 the guide in the docs ),注销就不会删除名称并不再显示登录按钮(在单击注销后立即)。仅当我单击指向另一个页面的链接时,才会发生更改。

当然我改变了配置(根据指南):

 services
   .AddIdentity<Data.Entities.ApplicationUser, IdentityRole>()
   .AddEntityFrameworkStores<ApplicationDbContext>()
   .AddDefaultTokenProviders();

有谁知道如何解决这个问题或者 DefaultUI 和脚手架类之间有什么区别?

最佳答案

有一个GitHub issue这描述了您遇到的确切问题。您在搭建脚手架之前间接使用的 Razor 类库 (RCL) 实现在 Logout.cshtml.cs 中有一个 OnPost 实现,看起来像 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 分支中有以下内容:

return Page();

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

关于c# - ASP.NET Core Identity - LoginPartial 在脚手架身份后损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56771862/

相关文章:

c# - MVC JsonResult 有数据,但浏览器没有

c# - 从剪贴板获取 Word ML

c# - 从 Azure Blob 存储返回 PDF 时出现“错误,无法加载 PDF 文档”

c# - View 中的 session 变量

c# - 对于 Azure Blob 存储 .NET 客户端, "Microsoft.Azure.Storage.DataMovement"和 "Azure.Storage.Blobs"之间有什么区别?

asp.net-core - ASP.Net Core Razor Pages 应用程序未绑定(bind)到模型中的属性

asp.net-core - Razor View 未生成 Razor 页面的 href

c# - 如何在 Razor Pages 中动态生成文件以供下载

c# - 带有方括号的模型绑定(bind) POST 值

c# - 无法访问继承的全局变量