dotnet / dotnet/aspnetcore

Event-like extensions for authentication.

Open
#58,812 0 comments 2 reactions 0 assignees View on GitHub
api-suggestion area-auth enhancement
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

## Background and Motivation

I'd like to run custom actions after authentication succeeds or fails. While some authentication options allow me to define callbacks for various events, there's no generic way to react to authentication process unless I implement `IClaimsTransformation` which has other intent.
In theory, custom middleware could work but authentication can be performed both by Authentication and Authorization middleware, so it must be enabled at correct place.

## Proposed API

I'd like to be able to register a custom implementation of `IAuthneticationEvents` (maybe multiple implementations):
```
public interface IAuthneticationEvents
{
// Always executed after authentication phase.
ValueTask AuthenticationCompleted(IReadOnlyDictionary resultByScheme);

// Executed if at least one scheme from the effective policy is successfully authenticated.
ValueTask AuthenticationSucceeded(IReadOnlySet succededSchemes, ClaimsPrincipal principal);

// Executed if neither of the schemes from effective policy was successfully authenticated.
ValueTask AuthenticationFailed(IReadOnlyDictionary resultByScheme);
}
```

## Risks

If I am not mistaken, the current architecture of authentication and authorization process makes implementing this functionality a bit hard. For example, authentication middleware always attempts to authenticate default scheme, but it is not aware of other authentications which will be executed by policy evaluator which belongs to authorization middleware.

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.