Skip to content

Add methods for registering both authentication and authorization middlewares/services #42047

Open
@captainsafia

Description

@captainsafia

Background and Motivation

To provide an abstraction of registering both authentication and authorization-related middlewares/services in an app with fewer lines of code and build on the foundation of automatically registering middlewares/services that we started in preview5, we would like to add extension methods for registering both authentication and authorization-related middlewares/services via one overload.

Proposed API

namespace Microsoft.Extensions.DependencyInjection;

public static class AuthServiceCollectionExtensions 
{
  public static IServiceCollection AddAuthenticationAndAuthorization(this IServiceCollection services);
  public static IServiceCollection AddAuthenticationAndAuthorization(
    this IServiceCollection services,
    Action<AuthorizationOptions> configureAuthorizationOptions);
  public static IServiceCollection AddAuthenticationAndAuthorization(
    this IServiceCollection services,
    Action<AuthenticationOptions> configureAuthenticationOptions);
  public static IServiceCollection AddAuthenticationAndAuthorization(
    this IServiceCollection services,
    Action<AuthenticationOptions> configureAuthenticationOptions,
    Action<AuthorizationOptions> configureAuthorizationOptions);
}
namespace Microsoft.AspNetCore.Builder;

public static class AuthAppBuilderExtensions
{
  public static IApplicationBuilder UseAuthenticationAndAuthorization(this IApplicationBuilder app)
}

Usage Examples

Before

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();

After

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthenticationAndAuthorization();
var app = builder.Build();
app.UseAuthenticationAndAuthorization();

After with Options

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthenticationAndAuthorization(options => {
  options.DefaultScheme = "foobar";
});
var app = builder.Build();
app.UseAuthenticationAndAuthorization();

Metadata

Metadata

Assignees

Labels

api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-authIncludes: Authn, Authz, OAuth, OIDC, Bearer

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions