dotnet / dotnet/aspnetcore

Add methods for registering both authentication and authorization middlewares/services

Open
#42,047 6 comments 0 reactions 0 assignees View on GitHub
api-needs-work area-auth
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

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

```csharp
namespace Microsoft.Extensions.DependencyInjection;

public static class AuthServiceCollectionExtensions
{
public static IServiceCollection AddAuthenticationAndAuthorization(this IServiceCollection services);
public static IServiceCollection AddAuthenticationAndAuthorization(
this IServiceCollection services,
Action configureAuthorizationOptions);
public static IServiceCollection AddAuthenticationAndAuthorization(
this IServiceCollection services,
Action configureAuthenticationOptions);
public static IServiceCollection AddAuthenticationAndAuthorization(
this IServiceCollection services,
Action configureAuthenticationOptions,
Action configureAuthorizationOptions);
}
```

```csharp
namespace Microsoft.AspNetCore.Builder;

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

## Usage Examples

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

**After**
```csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthenticationAndAuthorization();
var app = builder.Build();
app.UseAuthenticationAndAuthorization();
```

**After with Options**
```csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthenticationAndAuthorization(options => {
options.DefaultScheme = "foobar";
});
var app = builder.Build();
app.UseAuthenticationAndAuthorization();
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.