Very helpful for me. Im trying my best, but struggling to get the User credentials from an API endpoint.
Ive seen a lot of people use AuthenticationStateProvider but im having issues with it working on Blazor Server as scoped:
{
this.auth = auth;
}
var authState = await auth.GetAuthenticationStateAsync();
var user = authState.User;```
However its using AddSIngleton, which puts ALL users in a loop as i would need this as AddScoped which comes back with an error of :
'GetAuthenticationStateAsync was called before SetAuthenticationState.'
Whats the best (and suggested easiest) way i can get the Users info in a controller ??
THanks for all of your work on this :)
Hey @acusu , thank you for appreciating my article and welcome to the Auth0 Community!
Regarding your request, Iām not sure what is your scenario actually?
struggling to get the User credentials from an API endpoint.
Are you trying to get the userās credentials? From what API endpoint? And why?
This is not clear to me.
Maybe you want to implement an API endpoint returning the userās profile data. Is it so?
In that case, please, can you share your full implementation (better if a GitHub repo) to have more context? Thanks
Basically what I need is a way of recording user activity in my Database on all CRUD activities, So i would like the users Auth0 id so i can search the DB for the user and store accordingly (I use the Machine2Machine Auth0 to sync up userās)
If i use the singleton of :
services.AddSingleton<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
Sorry @acusu, I understand the issue you have due to using AddSingleton instead of AddScoped, but itās not clear the overall applicationās architecture to me.
Is your application a Blazor Server application? Why are you using Machine2Machine flow? Iām missing the overall picture, so Iām not sure I can help you. Sorry!
Anyway, taking a closer look at the code snippet you provided, it looks like you are using a non-initialized AuthenticationStateProvider instance:
private readonly AuthenticationStateProvider auth;
// maybe here is other code...
{
this.auth = auth;
}
var authState = await auth.GetAuthenticationStateAsync();
var user = authState.User;
Since I havenāt the full controllerās code, I canāt say what you are trying to do here. It looks like you declared your private auth variable and are trying to get the authentication state from it. That variable is not initialized, so it throws that exception.
Since you registered the AuthenticationStateProvider service, you need to inject that instance into your controller. You donāt have to declare a new variable.
At least, thatās what I guess from the information available to me.
Hey @acusu,
Unfortunately I donāt have an out-of-the-box solution for your problem.
I looked around and found many devs having the same problem.
The most authoritative discussion for this problem seems to be this one, but I donāt feel it provides a clear solution.
I read elsewhere that the Blazor authentication infrastructure cannot be used in API controllers and they suggest using HttpContext.User instead of GetAuthenticationStateAsync. You could try this way.
Hey folks, this blog post has been updated to .NET 6.0!
Also, the sample project uses the new Auth0 ASP.NET Core Authentication SDK now .
A lot of simplification!
I see in the forum here you planned to show us how to connect our Blazor Server to an API server. Can we please get a link to that? I keep finding tutorials on connecting ASP.NET Core Web API only and Blazor Server only, but not the combination of the two.
Hi @LuckyWolf19,
Thank you for joining the Auth0 Community.
These days Iām actually working on a tutorial showing how to call a protected API from an ASP.NET Core application. The sample application will be an ASP.NET Core MVC application, but the approach is the same for any ASP.NET Core application, including Blazor Server and Razor Pages apps.
I hope to publish it in the next few weeks. Thank you for your patience
I am struggling just a bit. I was able to get everything working just fine in my VS2022 debugger, but when I deployed the app to my development server, I started receiving a āCallback URL mismatchā error when attempting to log in. Upon closer inspection, I found that the absolute url being generated is an āhttpā scheme rather than the āhttpsā used to connect to the site (and is properly configured in my Auth0 Application settings). Iām concerned that this is because I have deployed the site behind an Apache reverse proxy server, so while the connection to Apache is https, the internal connection is http. And so the HttpContext is seeing an http connection rather than https, and so generating an absolute URL of the incorrect type because of it. Is there a way to override the automatically generated absolute URL and use one that I generate in my code? Iām also open to other explanations, of course.
Evidently, by setting āHttpContext.Request.IsHttps = true;ā before calling the ChallengeAsync method, I can force it to generate an https redirect_uri and all is well.
Iām happy you found a solution to your problem.
More in general, for issues related to reverse proxies, you may find helpful this document from Microsoft.
Maybe in your case, you could configure X-Forwarded-Proto header as described here and keep your code more environment-agnostic.
Of course, it adds a little more complexity to your code
I have found success to date. However, is there a mechanism by which I can add Claims in my code after the user has successfully logged in? I have seen such a mechanism when one is using the Open Id libraries, but there donāt appear to be any extension methods with that idea in mind from the Auth0 libraries. Or, at least, I havenāt been able to find it, and my Google Fu has thus far failed me.
Hi @lassanter,
If you want to add custom claims to the ID and/or access token right after the user has successfully authenticated but before your application receives it, you can use Actions.
Check this document to learn how to do it.
This blog post provides you with a practical example.
To enable role-based authorization in your Blazor application, follow the instructions in this document. I suggest using Actions to add the roles to the token instead of rules.
After this, you can use the authorization components as explained here.