Blazor C# equivalent to submit form method="post" action="/Account/Logout" #3129
Replies: 2 comments 4 replies
-
Blazor server or wasm? In Blazor server you normally have a logout page like this. And you navigate to that page like this. In Blazor wasm you don't have to log out the aspnetcore server - just set the current user in the authentication provider to an unauthenticated |
Beta Was this translation helpful? Give feedback.
-
Yes I had some trouble with that too because the logoff.cshtml is a razor view which is not included in the normal routes. Anyway, I took care of it like this...
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@{
SendToLogoff();
}
</NotAuthorized>
</AuthorizeRouteView> This causes any unauthorized UI to simply redirect user to logoff page. I've got a custom security policy that handles our custom authentication mechanism. Here is that method: private async Task SendToLogoff()
{
//several child components attempting to render will all trigger the unauthorized ui path above which calls SendToLogoff multiple times...we only need to process one
if (!_LogoffInProgress)
{
_LogoffInProgress = true;
//custom url helper method that creates a safe URL regardless of how site is deployed in IIS as root or subapp
var url = _jtNav.GetFullURL(Pages.LogoffModel.CONST_PAGEROUTE);
_JSRuntime.InvokeVoidAsync("locationChange", new object[1] { url });
}
}
public async Task<AuthenticationState> Logout()
{
var task = Task.Run(() =>
{
var unauthClaim = new ClaimsPrincipal();
return new AuthenticationState(unauthClaim);
});
_serverAuthProvider.SetAuthenticationState(task);
return await task;
}
That all seems to be working nicely for me. Hope this helps. P.S. I can't remember why, but I am purposely not awaiting the javascript method. I think there was some weird script error if I did. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to call the logout function with
NavigationManager.NavigateTo
or something else in C# code?Beta Was this translation helpful? Give feedback.
All reactions