开发者

ASP.NET Razor Pages: helperTag seems to not be updating after logout

I'm going through a tutorial on Razor Pages and trying to add functionality on the side.

I have just implemented a login/logout logic using c# within a partial view (_AccountInfoPartial.cshtml) to display only one of the corresponding two buttons in a dropdown menu, depending on whether the user is logged in or not (User.Identity.IsAuthenticated is true or false). The logout button is part of a form in this partial view, and the form submits a post request to Logout.cshtml.cs, where the logout is handled in OnPostAsync()

In OnPostAsync, after the signout is completed, I redirect to the same Logout.cshtml view, which checks whether the user is logged in or not, and displays text accordingly. I realize this is probably not very elegant. The reason I do this at the moment is to have the page display different content depending on the status of the user, so that I can handle the case when the user manually types in the URL of the logout page. In the case that this happens, I display a logout button on the logout page if the user is not logged out already, instead of logging him out directly. If he is already logged out, I give him that information by displaying "You have logged out."

Here is the relevant part of the partial view (_AccountInfoPartial.cshtml)

<ul class="navbar-nav">
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            @if (User.Identity.IsAuthenticated)
            {
                    @User.Identity.Name
            }
                
                <img src="~/Images/Account/login.png" class="menu-image" />
        </a>
        <ul class="dropdown-menu" aria-labelledby="navbarDropdown">

            @if (User.Identity.IsAuthenticated)
            {
                <li>
                    <form method="post" class="form-inline" asp-page="Account/Logout">
                        <button type="submit" class="dropdown-item">Logout</button>
                    </form>
                </li>
            }
            else
            {
                <li><a class="dropdown-item" asp-area="" asp-page="Account/Login">Login</a></li>
            }
        </ul>


    </li>
</ul>

Here is the OnPostAsync Method from Logout.cshtml.cs

 public async Task<IActionResult> OnPostAsync()
 {
     await HttpContext.SignOutAsync("MyCookieAuthentication");
     return RedirectToPage("/Account/Logout");
 }

Here is the Logout view (Logout.cshtml):

@page
@model ShagMeThatsAGreatRecipeRazor.Pages.Account.LogoutModel

<h1>Logout</h1>
@if(User.Identity.IsAuthenticated)
{
    <form method="post">
        <button type="submit" class="btn btn-primary" asp-page="Account/Logout">Logout</button>
    </form>

}
else
{
    <h3 class="text-center">You have logged out.</h3>
}

I'm getting the following behaviour, as illustrated by the screenshot further down in this post:

  • After I login, I click the logout button.
  • The OnPostAsync method is called, the cookie is deleted, and the user gets redirected to the (same) /Account/Logout.cshtml view.
  • The page picks up on the fact that the user is not authenticated anymore, and displays the correct content ("You have logged out.").
  • The partial view also seems to update correctly in the sense that it seems to pick up that the user is no longer authenticated, displaying t开发者_StackOverflow社区he Login text.
  • However, the helper tag (asp-page) is still pointing to Account/Logout (not, as I would hope, to Account/Login), as can be seen in the bottom left of the browser when mousing over the login-text.

ASP.NET Razor Pages: helperTag seems to not be updating after logout

  • If I click the login link (which is somehow pointing to the logout page), the OnPostAysnc method of Logout.cshtml.cs does not get called (my breakpoint is not hit), but the OnGet method of the same viewmodel does get called, and the page gets reloaded.
  • However, the page remains the same, with the Login text still (again) being presented in the partial view, but still pointing to the logout page.
  • If I click again, the whole thing repeats over and over.
  • However, when I then browse to another page within the app, the partial view does update, and the login button works again.

Since I am new to Razor Pages and MVVM, I have done some research into finding the cause of this. I have come across suggestions such as refreshing the partial view via Ajax (which I don't know how to do yet and would hope for this to be more simple) or updating the page via JavaScript (again something I was hoping would not be necessary)

My questions are:

  • Why is this happening? I would like to understand this, as I am sure it points to some gap in my understanding of Razor and/or web apps.

  • Is there some pattern/logic I can use with my existing page structure and architecture to get this to work correctly, without having to resort to Ajax or JS? Or would I need to revise my page

    structure/architecture or resort to either of the mentioned solutions, or something else entirely?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜