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:
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:
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.
Comments(0)