Asp.Net Core Blazor Logout does not work (redirecting to unavailable path)

Lukas Neubauer 26 Reputation points
2022-07-21T14:46:24.877+00:00

Hey there,

I'm facing a issue, that the user cannot log out, because the logout does get redirected to a path that's non existend.
I've tried scaffolding the identity framework again, but this didn't help either.

If I use the Account/LogOut or /Account/LogOut url the user gets redirected to: https://localhost:7172/Account/Login?ReturnUrl=%2FIdentity%2FAccount%2FLogOut.
I also tried Logout instead of LogOut. Didn't help either.
The route doesn't exist because I get the router defined message for there is nothing: Sorry, there's nothing at this address.

I'm using the logout call like this: (I'm using Mudblazor, thus the MudButton component usage. But this doesn't work with a default html button either)

<form method="post" action="Identity/Account/LogOut">  
    <MudButton ButtonType="ButtonType.Submit" Variant="Variant.Outlined" Color="Color.Info">  
        Ausloggen  
    </MudButton>  
</form>  

I'm using a Cookie authentication, that has been configured the following way:

builder.Services.AddAuthentication("Identity.Application").AddCookie();  
builder.Services.ConfigureApplicationCookie(options => { });  

And haven't changed the logout scaffolded .cshtml

@page  
@using Microsoft.AspNetCore.Identity  
@using Microsoft.AspNetCore.Authentication  
@attribute [IgnoreAntiforgeryToken]  
@inject SignInManager<WaWiIdentityUser> SignInManager  
  
@functions {  
  
    public async Task<IActionResult> OnPost()  
    {  
        if (SignInManager.IsSignedIn(User))  
        {  
            await SignInManager.SignOutAsync();  
        }  
  
        return Redirect("/");  
    }  
  
}  

Edit:

I checked the server answer, and as a result the client gets the status code 302 and the mentioned url: https://localhost:7172/Account/Login?ReturnUrl=%2FIdentity%2FAccount%2FLogout
I also tried setting the LogoutPath in the authentication and the applicationcookie but this doesn't help either.

builder.Services.AddAuthentication("Identity.Application")  
    .AddCookie(options => options.LogoutPath = "/Identity/Account/Logout");  
builder.Services.ConfigureApplicationCookie(options =>  
{  
    options.LogoutPath = "/Identity/Account/Logout";  
});  

Thanks ahead for any help!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,664 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,611 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Lukas Neubauer 26 Reputation points
    2022-07-21T17:20:21.743+00:00

    After a long search, I created the LogoutModel by myself, assigned the Logout.cshtml the LogoutModel and now it's working fine :)

    This is my final result:

    [AllowAnonymous]  
    [IgnoreAntiforgeryToken]  
    public class LogoutModel : PageModel  
    {  
        private readonly SignInManager<WaWiIdentityUser> _signInManager;  
        private readonly ILogger<LogoutModel> _logger;  
      
        public LogoutModel(SignInManager<WaWiIdentityUser> signInManager, ILogger<LogoutModel> logger)  
        {  
            _signInManager = signInManager;  
            _logger = logger;  
        }  
      
        public void OnGet()  
        {  
        }  
          
          
      
        public async Task<IActionResult> OnPost()  
        {  
            await _signInManager.SignOutAsync();  
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);  
            _logger.LogInformation("User logged out.");  
            return Redirect("Login");  
        }  
    }  
    
    1 person found this answer helpful.

  2. Jeremy 6 Reputation points
    2022-11-18T20:47:23.727+00:00

    For your final results I assume you created "LogOut.cshtml.cs"?

    What changes did you make to "LogOut.cshtml"?

    1 person found this answer helpful.
    0 comments No comments

  3. Андрей В 0 Reputation points
    2024-11-27T12:04:40.63+00:00

    my solution to this problem:

    LogOut.cshtml:

    @page
    @model LogoutModel
    

    and LogOut.cshtml.cs:

    using Microsoft.AspNetCore.Authentication.Cookies;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Authentication;
    [AllowAnonymous]
    [IgnoreAntiforgeryToken]
    public class LogoutModel : PageModel
    {
        private readonly SignInManager<IdentityUser> _signInManager;
        public LogoutModel(SignInManager<IdentityUser> signInManager)
        {
            _signInManager = signInManager;
        }
        public async Task<IActionResult> OnPost()
        {
            await _signInManager.SignOutAsync();
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return Redirect("Login");
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.