I have created a role for within an Azure App registration and assigned to me.
This is the assignment in the enterprise application
Now in the Blazor Client App,when i try to read the User roles assigned it is empty
var authstate = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authstate.User;
userName = user.Identity.Name;
var x = user.Claims.Where(t => t.Type == System.Security.Claims.ClaimTypes.Role).ToList();
The dependencyInjection is Program.cs looks like this
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("User.Read");
});
Update:
Program.cs
builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration)
.EnableTokenAcquisitionToCallDownstreamApi(new string[] { "user.read" })
.AddInMemoryTokenCaches();
// Add services to the container.
//builder.Services.AddControllersWithViews();
builder.Services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
})
// Add the Microsoft Identity UI pages for signin/out
.AddMicrosoftIdentityUI();
=====================================================
I can get the roles, as you can see, I have a role, and I assigned it to a user
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.