ASP.NET Core身份-脚手架身份后LoginPartial损坏 [英] ASP.NET Core Identity - LoginPartial broken after scaffolding identity

查看:114
本文介绍了ASP.NET Core身份-脚手架身份后LoginPartial损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a new Project from the VS 2017 template (web application with individual user accounts).

这会将ASP.NET Core身份添加为默认UI(使用来自nuget的UI).

This adds the ASP.NET Core Identity as default UI (using the UI from a nuget).

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

使用此nuget,一切都可以按预期进行.特别是loginPartial,它在用户登录后显示用户名,并在单击注销后立即显示登录"按钮.

With this nuget everything works as expected. Especially the loginPartial which shows the username once a user has logged in and shows the Login Button right after clicking logout.

一旦我搭建好布局并应用更改(根据

Once I scaffold the layout and apply the changes (according to the guide in the docs) the Logout does not remove the name and show the login button anymore (right after the click on logout). The change only happens when I click on a link to another page.

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

Of course I changed the configuration (according to the guide):

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

有人知道如何解决此问题,或者DefaultUI和脚手架类之间的区别是什么?

Does anyone know how to fix this or what the difference is between the DefaultUI and the scaffolded classes?

推荐答案

存在 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.

这篇关于ASP.NET Core身份-脚手架身份后LoginPartial损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆