C
C#9mo ago
Hrolgar

❔ Troubleshooting Blazor Server Authentication with Duende IdentityServer: Need Help and Insights!

Hello everyone! 👋 I've successfully set up a Duende IdentityServer that works seamlessly with Asp.Net Core as a frontend client. However, I'm facing some challenges when trying to use a Blazor Server client frontend instead. I've tried applying the same technique I use with an Asp.Net Core frontend, but it just doesn't seem to work. Here's the code I'm using in the Program.cs of the Asp.Net Core client:
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:5001";
options.ClientId = "web";
options.ClientSecret = "secret";
options.ResponseType = "code";

options.Scope.Clear();
options.Scope.Add("verification");
options.ClaimActions.MapJsonKey("email_verified", "email_verified");
options.Scope.Add("openid");
options.Scope.Add("profile");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
});
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:5001";
options.ClientId = "web";
options.ClientSecret = "secret";
options.ResponseType = "code";

options.Scope.Clear();
options.Scope.Add("verification");
options.ClaimActions.MapJsonKey("email_verified", "email_verified");
options.Scope.Add("openid");
options.Scope.Add("profile");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
});
Followed by:
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages().RequireAuthorization();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages().RequireAuthorization();
When I run the server and then the client, it successfully redirects to the server (at localhost:5001) while waiting for authorization. However, when attempting the same setup with a Blazor Server as a client, it doesn't work. Does anyone have any insights into why this might be happening, and any suggestions on how to make it work? I'd greatly appreciate any help or advice on this matter. 🙏
3 Replies
teauxfu
teauxfu9mo ago
i don't have any good links in front of me, but if you're doing auth from blazor server there's some extra hoops to jump through. the authentication / cookie setting step happens on a separate razor/mvc style cshtml page. there are some subtle differences with the way httpcontext is handled in blazor server i'd suggest looking for some docs on the AuthenticationStateProvider, that may be what you need, or help point in the right direction
Hrolgar
Hrolgar9mo ago
I'd love some examples of setting this up, if anyone have. Even better if anyone has done it with Duende Identity Server. (When I google I can only find examples for WASM).
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.