Kyr
Kyr
CC#
Created by Kyr on 8/12/2024 in #help
Blazor Web Assembly (stand alone) - [Authorize] Attribute not recognising roles straight after login
have a custom AuthenticationStateProvider that adds roles to the ClaimsPrincipal in public override async Task<AuthenticationState> GetAuthenticationStateAsync() As an anonymous user, when I hit a route protected with the [Authorize(Roles="Administrator")] attribute, the app correctly sends me to login and redirects me back to the protected route after login. However, when I log in as a user account that has the "Administrator" role, upon landing back on the protected route it tells me that I don't have permission to access that page (i.e. I'm logged in but don't have the correct role). If I reload the page in the browser, it then recognises that I have the role and lets me in. In my case, I'm adding the authorize attribute to a whole directory using _Imports.razor:
@using Microsoft.AspNetCore.Authorization
@layout AdminLayout
@attribute [Authorize(Roles = "GlobalAdministrator,Administrator")]
@using Microsoft.AspNetCore.Authorization
@layout AdminLayout
@attribute [Authorize(Roles = "GlobalAdministrator,Administrator")]
Not sure what I'm doing wrong.
49 replies
CC#
Created by Kyr on 7/12/2024 in #help
.NET 8 / Blazor Server+WASM / Configuration
In Blazor is there a way to pass configuration (i.e. appsettings / environment variables) from the Blazor Server project to the Blazor WASM project? In the .NET 8 Blazor template it creates two projects - one a Server project and one a WASM project. You just build and publish the Server project - it references the WASM project and that gets published along with it. I'm using .NET Aspire, which can only reference the server project in the AppHost. Aspire is setting environment variables (i.e. Base URL for a separate API) and I need the WASM project to get that to configure the named HttpClient in Program.cs. Any ideas / suggestions on how I can do this? UPDATE: I've really been trying to give Blazor a good shot in this project but I'm getting nowhere, fast and I'm just done with it. It's turned out to be just like every other time I've tried to get to grips with Blazor over the years. - hit a roadblock on something that, in my mind at least, seems like it should be relatively simple. - ask for help in various places, SO, Discord, etc. - Get zero help or support - Go back to NextJS and tell my team not to bother with Blazor still I really want to like Blazor because the concept is so nice. But its just not worth the stress at this point. In the time I've spent today trying to do this one simple thing I could've been half done with my app in NextJS, Vue or virtually any other JS framework. So frustrating.
3 replies
CC#
Created by Kyr on 6/28/2024 in #help
Properly output caching an API
I have a .Net 8 Web API (controllers) and I want to do the following: Cache the output of all successful GET requests (2xx status codes) VaryBy user VaryBy resource (controller) and endpoint (action) VaryBy querystring params (normalised order) When a PUT, POST, PATCH or DELETE on the same resource is successful, clear the GET caches for that controller for the same user Ideally I'd like this to happen automatically without needing me to write additional code in my controller actions each time. I thought I could achieve this with the built in output caching and it feels like it should be possible but I'm striking out on the how and looking for some guidance.
1 replies