ASP.NET Core 2.2 - Razor Pages Logout Behavior

I have scaffolded Identity to override IdentityUser, modify pages, add pages and just inspect the UI. In the new template, the logout function in _LoginPartial.cshtml is a post to the Identity/Account/Logout page with a returnUrl to /. This should redirect you to the home page but it doesn't. The Logout page is displayed stating you have logged out. This would not be so bad if it refreshed the nav bar to reflect your logged out status. This article will describe the functions and a remedy. I investigated reporting this issue on GitHub but discovered the community is still focused on the previous template. The first article I published to Code Project allowed me to upload images and then add them to my article. As I recall, it must been accomplished with Ajax because it appeared to be a partial page refresh. In my pursuit of learning ASP.NET Core Razor Pages I was determined to accomplish this behavior with model binding when composing and publishing my articles. This article and images are stored in the database and are dynamically displayed.

First the code snippet from _LoginPartial.cshtml:

<li class="nav-item">
    <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/", new { area = "" })" method="post" >
        <button type="submit" class="nav-link btn btn-link">Logout</button>
    </form>
</li>

Now the code snippet from Logout.cshtml.cs:

public async Task<IActionResult> OnPost(string returnUrl = null)
{
    await _signInManager.SignOutAsync();
    _logger.LogInformation("User logged out.");
    if (returnUrl != null)
    {
        return LocalRedirect(returnUrl);
    }
    else
    {
        return Page();
    }
}

This logout behavior returns:

Logged Out Before.

The remedy is edit Logout.cshtml.cs > OnPost() to refresh the Logout page:

public async Task<IActionResult> OnPost(string returnUrl = null)
{
    await _signInManager.SignOutAsync();
    _logger.LogInformation("User logged out.");
    if (returnUrl != null)
    {
        return LocalRedirect(returnUrl);
    }
    else
    {
        //return Page();
        return RedirectToPage();
    }
}

This logout behavior returns:

Logged Out After.

A little bonus, edit Hello USERNAME! in _LoginPartial.cshtml:

<li class="nav-item">
    <a class="nav-link" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
</li>

To Manage USERNAME:

<li class="nav-item">
    <a class="nav-link" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Manage @User.Identity.Name</a>
</li>

I realize this is a low priority for the open source community to address. This article was more about uploading, storing and retrieving the screenshots with Entity Framework Core and Razor Pages. The article references are more about process than the article.

Article references:
For the Inspiration. Code Project
Ken Haggerty
Created 01/10/19
Updated 02/21/19 23:34 GMT

Log In or Reset Quota to read more.

Article Tags:

Identity Scaffold
Successfully completed. Thank you for contributing.
Processing...
Something went wrong. Please try again.
Contribute to enjoy content without advertisments.
You can contribute without registering.

Comments(0)

Loading...
Loading...

Not accepting new comments.

Submit your comment. Comments are moderated.

User Image.
DisplayedName - Member Since ?