I just went through the process of adding Auth0 to a Blazor Server application using the Blazor Server Quickstart. I had some issues and figured I’d report them so that they can be fixed and the next developers to use the Quickstart have an easier time.
The Blazor Server Quickstart I’m referencing throughout: Add Login to Your Blazor Server Application - Auth0 Docs
-
Step 3 says to add the Client Secret to appsettings.json. This works, but it does mean that the Client Secret could be exposed if the developer commits their code to a remote repository. It may be worth adding a note mentioning ASP.NET’s secrets.json feature: Safe storage of app secrets in development in ASP.NET Core | Microsoft Learn
-
Step 5 says to add 6 files to handle login and logout. Some of those are Razor pages, which seem a bit out of place in a Blazor app. In my opinion, this is a lot of extra fluff and clutter that is unnecessary. I believe a simpler and easier option would be to use Minimal API endpoints to map /login and /logout. Here’s the code I used in my own app. This code can be added to Program.cs directly or split off into a separate file.
app.MapGet("/Login", async (HttpContext httpContext, string returnUrl = "/") =>
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();
await httpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
});
app.MapGet("/logout", async (HttpContext httpContext) =>
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri("/")
.Build();
await httpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
await httpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
});
This code has the advantage of being less work to set up, fewer files in the repo, and less boilerplate while accomplishing the same thing.
-
When I referenced a particular step to the on-page AI, it kept getting the step number wrong. For example, if I say “Which step says to add values to appsettings.json?”, it replies with “Step 2: Setup Auth0 Application Configuration is the step that instructs you to add your Auth0
Domain,ClientId, andClientSecretvalues toappsettings.json.” (it’s actually step 3). The AI consistently said the wrong step number every time it mentioned a step. This came up a few times during normal queries, not just a direct question like this. -
The Sample Application link at the bottom of the guide https://github.com/auth0-samples/auth0-aspnetcore-blazor-server-samples/tree/main/Quickstart/Sample is not relevant to this guide. It uses an old version of .NET and doesn’t follow the steps in the Quickstart guide, so the code is significantly different from what the guide says to do. It seems like the guide was updated, but the sample app wasn’t.
I hope this is helpful!