question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DI support for auth handler options

See original GitHub issue

https://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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
Tratchercommented, Feb 19, 2020

Oh, right, there’s a named overload:

services.AddOptions<JwtBearerOptions>("MyJwt")
             .Configure<ILogger<CustomDataFormat>>((options, logger) =>
             {

             });
1reaction
Tratchercommented, Feb 19, 2020

You still need to call AddJwtBearer for other reasons, it’s wiring the handler into the auth service.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found