skip to Main Content

I have created a role for within an Azure App registration and assigned to me.

This is the role
enter image description here

This is the assignment in the enterprise application

enter image description here

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:

enter image description here

2

Answers


  1. 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

    enter image description here

    enter image description here

    enter image description here

    Login or Signup to reply.
  2. To access the Azure AD API, you have to grant your app with right permissions. On your API Application go to API Permissions page, select Grant admin consent.
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search