asp.net-coreoauth-2.0blazoropenid-connectduende-identity-server

Blazor authentication not working with open id connect duende SSO


I have 1 blazor client and 1 SSO server that uses duende identity server. Current project version is .Net7. When accessing blazor's index application link at https://localhost:7244 will automatically redirect the login to my SSO. It works fine. But when I log out at SSO. Blazor client's cookie check also deletes some information like SSO. Logout completed. But blazor client still doesn't redirect to login page automatically. I had to F5 the website again for it to work. Is there a way to automatically navigate to the login page after logged out at SSO ? Here are some of my configurations:

SSO Config in program.cs

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<BeaconSSOContext>();

builder.Services.AddIdentityServer()
                .AddInMemoryClients(new Client[] {
                    new Client
                    {
                        ClientId = "client",
                        AllowedGrantTypes = GrantTypes.Implicit,
                        RedirectUris = { "https://localhost:7244/signin-oidc" },
                        PostLogoutRedirectUris = { "https://localhost:7244/signout-callback-oidc" },
                        FrontChannelLogoutUri = "https://localhost:7244/signout-oidc",
                        AllowedScopes = { "openid", "profile", "email", "phone" }
                    }
                })
                .AddInMemoryIdentityResources(new IdentityResource[] {
                    new IdentityResources.OpenId(),
                    new IdentityResources.Profile(),
                    new IdentityResources.Email(),
                    new IdentityResources.Phone(),
                })
                .AddAspNetIdentity<IdentityUser>();

Blazor client (program.cs):

// Add services to the container.
builder.Services.AddRazorPages();

builder.Services.AddServerSideBlazor();

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = "cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("cookies")
.AddOpenIdConnect("oidc", options =>
{
    options.Authority = "https://localhost:7001";
    options.ClientId = "client";
    options.MapInboundClaims = false;
    options.SaveTokens = true;
});

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
});

builder.Services.AddAntDesign();

ConfigurationHelper.Initialize(builder.Configuration);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();

app.UseAuthorization();

app.MapBlazorHub();

app.MapFallbackToPage("/_Host");

app.Run();

Some solutions that I have tried:


Solution

  • Here's a full example in this issue:

    Redirect manually after HttpContext.SignOutAsync();

    public class LogoutModel : PageModel
    {
        public async Task<IActionResult> OnGetAsync()
        {
            await HttpContext.SignOutAsync();
            return Redirect("/");
        }
    }
    

    Redirect you to Login Page if you are not authorized in app.razor:

    <NotAuthorized>
        @{
            var returnUrl = 
            NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
            NavigationManager.NavigateTo($"login?redirectUri= 
                                  {returnUrl}", forceLoad: true);
         }
    </NotAuthorized>