ASP.NET Core Identity - LoginPartial broken after scaffolding identity

一笑奈何 提交于 2019-12-24 02:59:07

问题


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

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

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

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();

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


回答1:


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();
    }
}

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();

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.



来源:https://stackoverflow.com/questions/56771862/asp-net-core-identity-loginpartial-broken-after-scaffolding-identity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!