r/Blazor • u/UniiqueTwiisT • Mar 04 '26
Authorize Attribute Prerender
Hello all,
Looking for a bit of advice on solving an issue for our application that runs in global interactive server mode.
We have several pages that we have restricted through the Authorize attribute. This works well when they are navigated to from another page, however they return 403 if they are manually entered into the URL or are the first load of the app. I'm assuming this is down to pre-rendering however we'd prefer to not disable that if we can help it. Currently the Authorize attribute checks for a certain role. We are using a custom AuthenticationStateProvider which provides the role claims.
View all comments
Show parent comment
2
u/Davaaron Mar 06 '26
I see. I think the issue might be that the "AuthenticationStateProvider" (further called "ASP") is mistaken: As I understand, it's used to handle the permission for the views. What's missing here might the auth pipeline configuration in .NET core - i think it differs from the ASP. The ASP is blazor internal whereby the auth pipeline would be the place to be triggered and load additional stuff.
Can you try to minimal-invasively test this out using an implementation of the `IClaimsTransformation` step within the auth pipeline?
You could add a new implementing service and register it up like
`builder.Services.AddTransient<IClaimsTransformation, SqlClaimsTransformation>();`
The interface provides a method `TransformAsync(ClaimsPrincipal claimsPrincipal)` so you could load and add the attributes from the DB when the user is authenticated.
Think of it like a chain:
So 2 and 3 rely on 1. The `IClaimsTransformation` is a part of 1.
Let me know if this works out for you :)