0

Considering the following jwt token:

enter image description here I use the following code to protect my aspnet core webapi:

    builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApi(jwtBearerOptions =>
    {
        configuration.Bind("AzureAdB2C", jwtBearerOptions);
    },
    microsoftIdentityOptions => { configuration.Bind("AzureAdB2C", microsoftIdentityOptions); });

How can I add validation to above code to validate the azp calim?

2 Answers 2

0

You can leverage Policy-based Authorization to achieve this. In your case, a simple function policy should help check the azp claim.

By default, you need to use this policy in Authorize attributes on your controllers, pages, or endpoints. Since you might want this for all endpoints, you could add it as an authorization filter.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use directly the event OnTokenValidated in the JwtBearerEvents when setup the AddMicrosoftIdentityWebApi().

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApi(jwtBearerOptions =>
    {
        configuration.Bind("AzureAdB2C", jwtBearerOptions);

        jwtBearerOptions.Events = new JwtBearerEvents()
        {
            OnTokenValidated = async ctx =>
            {
                if (((JsonWebToken)ctx.SecurityToken).Azp != "< The AZP value to check >")
                {
                    ctx.Fail("Invalid authorized party.");
                }

                await Task.CompletedTask;
            },
        };
    },
    microsoftIdentityOptions => { configuration.Bind("AzureAdB2C", microsoftIdentityOptions); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.