最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

.net - Add custom JwtBearerHandler to "AddMicrosoftIdentityWebApi" in .net7 - Stack Overflow

matteradmin6PV0评论

How does one wire a custom JwtBearerHandler in .NET7 to this:

 builder.Services.AddAuthentication() .AddMicrosoftIdentityWebApi(builder.Configuration);

Alternatively, how would I translate the previous statement into this

builder.Services
.AddAuthentication()
.AddScheme<JwtBearerOptions, CustomJwtHandler>("AzureAd", options => Configuration.Bind("AzureAd", options))

like they do in this example in the answer :

How does one wire a custom JwtBearerHandler in .NET7 to this:

 builder.Services.AddAuthentication() .AddMicrosoftIdentityWebApi(builder.Configuration);

Alternatively, how would I translate the previous statement into this

builder.Services
.AddAuthentication()
.AddScheme<JwtBearerOptions, CustomJwtHandler>("AzureAd", options => Configuration.Bind("AzureAd", options))

like they do in this example in the answer : https://stackoverflow/a/68258618/441365

Share Improve this question edited Nov 19, 2024 at 3:13 Qiang Fu 9,4371 gold badge6 silver badges16 bronze badges asked Nov 18, 2024 at 16:41 user441365user441365 4,03411 gold badges46 silver badges63 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You could directly replace built-in JwtBearerHandler by CustomJwtBearerHandler using DI.

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddTransient<JwtBearerHandler, CustomJwtBearerHandler>();

You could test logic like following which will work in the "MicrosoftIdentityWebApi" pipleline.

    public class CustomJwtBearerHandler : JwtBearerHandler
    {
        public CustomJwtBearerHandler(
            IOptionsMonitor<JwtBearerOptions> options,
            ILoggerFactory logger,
            System.Text.Encodings.Web.UrlEncoder encoder,
            ISystemClock clock)
            : base(options, logger, encoder, clock)
        {
        }

        protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
        {
            // Perform your custom authentication logic here
            var result = await base.HandleAuthenticateAsync();

            if (result.Succeeded)
            {
                // Custom logic, e.g., logging or additional validation
                var ticket = result.Ticket;

                // Example: Reject if a custom claim is missing
                if (!ticket.Principal.HasClaim(c => c.Type == "custom-claim"))
                {
                    return AuthenticateResult.Fail("Missing required custom claim.");
                }
            }

            return result;
        }
    }
Post a comment

comment list (0)

  1. No comments so far