.Net 8.0 Blazor

In my case it’s a Blazor Server app. I keep getting the following (which then leads to 401 error) when I try to connect to the Hub via a Razor component:

dbug: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[9]
      AuthenticationScheme: Cookies was not authenticated.
dbug: Microsoft.AspNetCore.Authorization.AuthorizationMiddleware[0]
      Policy authentication schemes  did not succeed

I am not really sure if I really need an access token as the error text underlines cookies.

Hey @quinten.de.clerck,
I replied to the issue you opened.

Hi @andrea.chiarelli and @quinten.de.clerck

I added a comment with a workaround.

1 Like

Thanks for another good article @andrea.chiarelli.

1 Like

Thank you @grantcolley. I’ll update the code snippet in the article

Hi @andrea.chiarelli ,

Would you be able to guide to how to implement the same blazor .net 8 in Class library ?

I actually have a solution with multiple projects each serves separate purpose. But the authentication will be same as these all projects (blazor web applications) will connect to same database. How can I implement authentication once in class library and use it in all projects and share sessions ?

Hi @hunainnasirbscs,
I’m not sure to understand your scenario. Please, can you clarify what do you mean by “I have a solution with multiple projects each serves separate purpose”? Do you have multiple independent Blazor applications in one solution? :thinking:
Can you elaborate a little bit more on your scenario?

Hi! I’ve been having the same issue and Microsft docs are really a mess for this particular scenario. The issue is covered here: Server-side ASP.NET Core Blazor additional security scenarios | Microsoft Learn, but I tried this and the state is not persistent. TokenProvider always has null values for me. I ended up using a custom service that calls the API and that custom service receives an IHttpAccessor via DI. I use that IHttpAccessor to obtain the bearer token and using it to call the API. I’m not sure if there is a better way, but this works for me!

I want to have all authentication code (including login\registration\authentication pages) in Razor Class Library project (.net 8). The razor class library will then be compiled and referenced as dll by Blazor Web Applications (.net 8).

I will have multiple independent applications (Admin, Touch, Report, Dashboard etc). They will be using same database, same user and roles structures. Means share login, signup auth rules and logic. This is the reason to avoid replication and avoid API usage I want to move code to centralized dll and use. Hope this clears the scenario.

@andrea.chiarelli In your article you describe how to authenticate with Auth0 in Blazor 8 - how do we send users to the Signup screen of Universal Login?

I tried adding a screenhint parameter with no luck:

app.MapGet("/Account/Register", async (HttpContext httpContext, string redirectUri = "/hello") =>
    var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
        .WithScope("profile email")
        .WithParameter("screenhint", "signup")

    await httpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);

Also I need to extract the user’s sub from the claims when returning from universal login, how do I intercept this before my redirect URL loads? In React I was using a callback endpoint, do I need the equivalent? So I set redirectUrl to something like /callback and make a razor componment to do something in there?

Hi @nexusrob,
To enable the signup screen of Universal Login, you have to use the screen_hint parameter, as documented here. Your code is using a misspelled parameter.

To customize the ID token handling, you can configure the AddAuth0WebAppAuthentication() extension method as in the example shown here. The Auth0 SDK is a wrapper around the Microsoft.AspNetCore.Authentication.OpenIdConnect library.

I hope this helps.

Yep screen_hint worked immediately, and the other link is helpful too. Thanks very much @andrea.chiarelli

1 Like