-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Blazor with multiple auth middleware #14916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@aL3891 Thanks for contacting us. Does the issue happen when you are trying to render the initial page or after the first page has rendered and the Blazor app is trying to initialize? Blazor simply uses whatever user is on HttpContext.User at the time it renders the initial document (for prerendering) or whatever user is in HubContext.User when it starts the circuit. I suspect you have some miss-configuration by which your user is not being correctly setup in one of those two situations. To troubleshoot the issue you can do a couple of things:
The issue at the end of your comment is unrelated. For more info check https://docs.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.0&tabs=visual-studio |
This issue happens after the intial render. _host and other non-blazor routes does authenticate correctly and does have the user set. It's only blazor components that do not, and only when multiple auth middleware is used. Disabling bearer auth causes everything to work correctly, as does setting There may be some configuration that would solve this, that'd be great :) but i havent found any Did you try the steps i provided? |
as i mentioned, i have a suspicion that this is caused by blazor (or signalr) is not calling all the auth middlewares, only the first one. for regular routes, this is solved by
or by setting [Authorize(AuthenticationSchemes ="AzureAD,AzureADBearer")] on controllers/pages. This does not seem to be supported on blazor components though, doing so gives an exception
Perhaps there is a way to specify the equivalent setting for blazor/signalr on a global level? |
Ah, I think I see it now. You don’t seem to be setting the default authentication scheme. You need to set that in the authentication options. Also, you can set the authorization options for blazor by adding metadata to the endpoints, you can do so by chaining the call after the call to MapBlazorHub endpoints.MapBlazorHub()
.RequireAuthorization(/* Policy */); |
I can't set the default scheme since that seems to mess up the multiple auth as i mentioned. Or atleast i have not found a way to get it to work when setting it. Using
👍 I think that should be added to the blazor authorization docs as well since its useful to force all blazor communication to be authorized, especially for backoffice apps like the one i'm building |
@aL3891 I'm glad that worked out. I'm closing the issue as there is nothing Blazor specific here. I've filed a docs issue to cover this topic as part of the routing docs. |
Describe the bug
When using multiple auth middleware (AzureAD and AzureAdBearer in my case) blazor seems unable to find the logged in user. The user is logged in though, its presented with the azure login ui and visiting a non-blazor page does correctly show the logged in user.
To Reproduce
Steps to reproduce the behavior:
Create a new blazor server app in visual studio, with work/School auth.
In Startup.cs, replace the
AddAuthentication
andAddControllersWithViews
calls with the followingRun and login
Expected behavior
The blazor app should show the logged in user but blazor in particular seems unable to do so.
looking at the output [with some static file stuff removed]
It seems like authorization is successful when serving the _host but then fails later.
Additional context
There are few significant notes about this setup. AzureAD and AzureADBearer is used to use the AzureAD challenge if no auth is provided. In other words, you're ment to be able to open the app in a browser and enter the user login flow, or for a script to make a call to an api using a bearer token as well.
This means the auth filter must specify the Bearer auth first and the AzureAD second, otherwise the Bearer auth overwrites the AzureAD challenge response as user browses to a page. (at least this is my understanding)
i notice though that the log only prints out one auth middleware at the end of the log there, so i wonder if blazor only checks the first auth middleware, the bearer in my case, witch isn't authorized while missing the auth middleware that actually is authorized, i'm just guessing here though
The first request looks like this and calls both auth middlewares
i'm using the latest vs preview and 3.0.100 of the dotnet sdk
This issue also seems a bit similar to #13709 but not quite the same
The text was updated successfully, but these errors were encountered: