Blazor Web App (.NET 8) - Logout functionality with IdentityCore

What's the best way to implement log out functionality? Looks like I can't use signInManager.SignOutAsync() on an InteractiveServer application
4 Replies
viceroypenguin
viceroypenguin10mo ago
I use this a simple link to /logout will trigger the logout, and the auth infra will redirect back to the login page
Maverick (Shaun)
Maverick (Shaun)OP10mo ago
I see you're using HttpContext there, but isn't that unavailable when using interactive server?
viceroypenguin
viceroypenguin10mo ago
yes. that's why i'm using a minimal apis endpoint here. to force a page-cycle which will have httpcontext. just because the source page is interactive server doesn't mean all pages are interactive server if you have a link from a blazor page to something else, it'll force a page load like any other
Maverick (Shaun)
Maverick (Shaun)OP10mo ago
I see - thanks for the tip, I'll have a go at implementing it and see if I can get it working That worked, thank you. Using:
public static void MapAccountServices(this IEndpointRouteBuilder app)
{
app
.MapGet("/Logout", async (HttpContext context, string returnUrl = "/") =>
{
await context.SignOutAsync(IdentityConstants.ApplicationScheme);
context.Response.Redirect(returnUrl);
})
.RequireAuthorization();
}
public static void MapAccountServices(this IEndpointRouteBuilder app)
{
app
.MapGet("/Logout", async (HttpContext context, string returnUrl = "/") =>
{
await context.SignOutAsync(IdentityConstants.ApplicationScheme);
context.Response.Redirect(returnUrl);
})
.RequireAuthorization();
}
and then navigating to the Logout route while forcing a refresh with: NavigationManager.NavigateTo("/Logout", true); Thanks for the help!
Want results from more Discord servers?
Add your server