I have a client app developed in webassembly blazor and protected by Azure AD where its registered and 3 roles are defined and assigned to users.
Once a user sign-in the user is being redirect to profile page where set of claims are displayed
Claim Type Value
oid 091fadf9-b0bd-4583-b55d-XXXX
preferred_username XX
roles ["Administrator"]
Based on user role different UI will be shown, however this didnt work
<AuthorizeView Roles="Administrator">
ADMIN UI
</AuthorizeView>
my program class looks like this:
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
//builder.Logging.SetMinimumLevel(LogLevel.Debug);
////builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<CustomAuthorizationMessageHandler>();
builder.Services.AddHttpClient("myAPI",
client => client.BaseAddress = new Uri("private API URL"))
.AddHttpMessageHandler<CustomAuthorizationMessageHandler>();
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
.CreateClient("myAPI"));
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add(myAPI);
options.UserOptions.RoleClaim = "roles";
});
await builder.Build().RunAsync();
}
I have a supported class for user:
public class UserClaimsBase: ComponentBase
{
// AuthenticationStateProvider service provides the current user's ClaimsPrincipal data.
[Inject]
private AuthenticationStateProvider AuthenticationStateProvider { get; set;
}
protected string _authMessage;
protected IEnumerable<Claim> _claims = Enumerable.Empty<Claim>();
// Defines list of claim types that will be displayed after successfull sign-in.
private string[] returnClaims = { "name", "preferred_username", "tid", "oid", "roles" };
protected override async Task OnInitializedAsync()
{
await GetClaimsPrincipalData();
}
/// <summary>
/// Retrieves user claims for the signed-in user.
/// </summary>
/// <returns></returns>
private async Task GetClaimsPrincipalData()
{
// Gets an AuthenticationState that describes the current user.
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
// Checks if the user has been authenticated.
if (user.Identity.IsAuthenticated)
{
_authMessage = $"{user.Identity.Name} is authenticated.";
// Sets the claims value in _claims variable.
// The claims mentioned in returnClaims variable are selected only.
_claims = user.Claims.Where(x => returnClaims.Contains(x.Type));
}
else
{
_authMessage = "The user is NOT authenticated.";
}
}
}
What could be wrong here ? Im not sure what im missing ? Any help is appreciated.