简体   繁体   中英

How to get access token claims in a Blazor Server app using OIDC?

In a Blazor server application that uses OIDC, the following code can access the authentication state and user claims.

@code {
    [Inject] AuthenticationStateProvider AuthStateProvider { get; set; }

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthStateProvider.GetAuthenticationStateAsync();
        var claims = authState.User.Claims;
    }
}

claims contain the following 索赔

However, the access token returned from the OIDC provider had many more claims such as scope , client_id , aud , etc.

How to access all claims in the access token from a Blazor Server component?

by default the OpenIDConnect handler will filter out and only include some specific claims. To map other claims, then you need to map them one by one using

AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
                {
           
                    options.ClaimActions.MapUniqueJsonKey("myclaim", "myclaim");
                    options.ClaimActions.MapUniqueJsonKey("role", "role");
...

See this resource Mapping, customizing, and transforming claims in ASP.NET Core

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM