DI support for auth handler options
See original GitHub issuehttps://github.com/dotnet/aspnetcore/issues/9337#issuecomment-581489114 https://github.com/dotnet/aspnetcore/issues/9337#issuecomment-581516787
Describe the bug
The authentication handlers are configured in Startup.ConfigureServices and have trouble relying on services for any customization. E.g.
.AddCookie(o =>
{
o.TicketDataFormat = new CustomDataFormat(settings.Secret, /* Add logging? */);
});
IOptions supports DI for configure callbacks, but the existing overloads aren’t compatible with the auth setup pattern.
Workaround:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
services.AddTransient<IConfigureOptions<CookieAuthenticationOptions>>(sp =>
new ConfigureNamedOptions<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme, o =>
{
o.TicketDataFormat = new CustomDataFormat("secret", sp.GetRequiredService<ILogger<CustomDataFormat>>());
}));
Proposals:
Some new overloads could make this easier. We should consider if these apply to all of the auth handlers, not just the Cookie samples given here.
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, (o, serviceProvider) =>
{
o.TicketDataFormat = new CustomDataFormat("secret", sp.GetRequiredService<ILogger<CustomDataFormat>>());
});
.AddCookie<ILogger<CustomDataFormat>>(CookieAuthenticationDefaults.AuthenticationScheme, (o, logger) =>
{
o.TicketDataFormat = new CustomDataFormat("secret", logger);
});
services.Configure<CookieAuthenticationOptions, ILogger<CustomDataFormat>>(CookieAuthenticationDefaults.AuthenticationScheme, o =>
{
o.TicketDataFormat = new CustomDataFormat("secret", sp.GetRequiredService<ILogger<CustomDataFormat>>());
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:16 (16 by maintainers)
Top Results From Across the Web
Dependency injection in requirement handlers in ASP.NET ...
Learn how to inject authorization requirement handlers into an ASP.NET Core app using dependency injection.
Read more >Policy-based authorization in ASP.NET Core
Learn how to create and use authorization policy handlers for enforcing authorization requirements in an ASP.NET Core app.
Read more >Resolve dependencies for authentication event handler ...
1 Answer. The authentication system in ASP.NET Core 2 supports this natively using the EventsType property of the authentication scheme options ...
Read more >Creating an authentication scheme in ASP.NET Core 2.0
How to make authentication handlers in ASP.NET Core 2.0, and walks through a naive implementation for HTTP Basic authentication.
Read more >Authentication handler in ASP.Net Core (JWT and Custom)
An authentication handler is a class, where we will define how to react to a specific scheme. To implement a handler, we will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Oh, right, there’s a named overload:
You still need to call AddJwtBearer for other reasons, it’s wiring the handler into the auth service.