I am using CustomAuthentication in order to validate weather a user has access to view a certain page
At the moment my page is able to be restricted based on
@attribute[Authorize]alone
But I want to be able to authorize based on roles
@attribute[Authorize(Roles="admin")]
when I apply this it gives me a 403 forbidden error
My CustomAuth Class fetches the role from a DB like so
public class AuthService : AuthenticationStateProvider{ private HttpClient _client; public AuthService(HttpClient client) { _client = client; Console.WriteLine(_client.BaseAddress); } public override async Task<AuthenticationState> GetAuthenticationStateAsync() { try { var person = System.Security.Principal.WindowsIdentity.GetCurrent()?.Name; string temp = "personnamehere"; var userinfo = await _client.GetFromJsonAsync<RoleClass>($"/api/BlazorAppAuthService/Users/{temp}"); if (userinfo is null) { return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())); } var claims = new List<Claim> { new Claim(ClaimTypes.Name, person), new Claim(ClaimTypes.Role, userinfo.Role.ToLower()) }; var identity = new ClaimsIdentity(claims, "Windows"); var user = new ClaimsPrincipal(identity); foreach (var claim in claims) { Console.WriteLine($"{claim.Type} : {claim.Value}"); } var authState = new AuthenticationState(user); NotifyAuthenticationStateChanged(Task.FromResult(authState)); return authState; // var role = await _client.GetFromJsonAsync<string>(""); } catch (Exception ex) { return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())); } }//end of function}}
I am using a temp value that is stored in my db just in order to pull the role. This is temporary
Program.cs (snippet of applicable lines)
builder.Services.AddAuthorizationCore();
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
var app = builder.Build();
app.UseAuthentication();app.UseAuthorization();
My settings allow for windows Authentication
My app.razor contains the tags
Again it can work on Authorize alone but does not work based on roles.
Any help would be appreciated