Just two notes from when I was adding these helpful code bits to my project that may help others:
If you’re just going to connect from the wasm to the host, extend BaseAddressAuthorizationMessageHandler instead of AuthorizationMessageHandler. It sets the URL for you so you don’t need the ConfigureHandler() call.
You need to put your MessageHandler into the services, e.g.
builder.Services.AddScoped<Auth0MessageHandler>();
builder.Services.AddHttpClient("ServerAPI", client =>
client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<Auth0MessageHandler>();
I was more thinking along the lines of having a button to go register and another to login within my UI which would redirect the user to the relevant page of the universal login.
I done something similar in an MVC application using the following code:
[AllowAnonymous]
public async Task SignUp(string returnUrl = "/")
{
var properties = new AuthenticationProperties { RedirectUri = returnUrl };
properties.Items.Add("screen_hint", "signup");
await HttpContext.ChallengeAsync("Auth0", properties);
}
Is there a way to pass this screen_hint property in Blazor?
Hey @nathanuel81,
I didn’t try, but I think you can do it by handling the OnRedirectToIdentityProvider event (see here), similarly to how you are handling the logout redirection.
Let me know if it works.
I’ve successfully added Auth0 authentication to my existing Blazor Server application.
Authentication (login/logout/…) & views are authorized properly. But how could I protect the back-end controllers which are also part of the application?
Could I easily get the token from the razor pages and pass it through the controller ?
f.e. via httpclient
That’s what the message handler is doing for you. Appending the Bearer token into the headers. Then you just need to put the JWT bearer bits in the server.
Thank you. I’ve created this handler however, I receive the following error
.Auth0MessageHandler’: Unable to resolve service for type ‘Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider’ while attempting to activate
Shouldn’t this Interface be resolved by the framework itself? Do I need to manually resolve this?
Creating a Blazor Server app & using Typed HttpClient
services.AddScoped<Auth0MessageHandler>();
services.AddHttpClient<ApiService>(client =>
{
client.BaseAddress = new Uri("https://localhost:44364/");
}).AddHttpMessageHandler<Auth0MessageHandler>();
Firstly, sorry if this a rookie question, I’ve never had to deal with authentication before.
I’m creating a Blazor Server Side application, and implemented Auth0 using the following blog post by Auth0:
I’ve created a user and added a property in the App_Metadata, to let me set information for the user but stuff they can’t change (CompanyId: 3).
How do I get the CompanyId from the App_Metadata in Auth0 into my application? It’s not a role, but gives me information about the user to link them to more data in my own database.
Hi @dan6,
Welcome to Auth0 Community and thanks for reading this blog post.
Regarding your question, in general, you should find your user’s data in the User.Identity object. However, there are two points to point out:
By default, the User.Identity object is not automatically populated. You should apply a few changes to your code in order to map user’s data to that object. Please, take a look at this to get more info.
The user’s data that populate the User.Identity object comes from the ID token issued by Auth0. However, these data are just the standard OIDC claims.
To include additional information as part of the ID token (the App_Metadata, for example), you need to add custom claims through a rule.
Please, check out these documents to learn more:
Hi @ehilimire,
Welcome to the Auth0 Community!
An update to this article is under consideration. I will announce it here when it becomes available (hopefully very soon).
Thank you for your interest