dotnet / dotnet/aspnetcore

Write custom authentication failure information to the response body

Open
#44,100 9 comments 8 reactions 0 assignees View on GitHub
area-auth
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I have written my Authentication Handler (loosely based on JwtBearHandler). Obviously, I have some `AuthenticateResult.fail(ex)`. All that is good. But, I would like to extract info from these errors and translate them to the Response body aside from the expected 401.

Outside the authentication / authentication framework, I've written an exception handler that does exactly that for controllers.

Unfortunately, I've failed to find a way to retrieve the `AuthenticateResult` produced by the authentication Handler. `IAuthorizationMiddlewareResultHandler` do expose the `AuthenticateResult` but not when the authentication fails, only when it succeed, through `HttpContext.Features.Get()`.

### Describe the solution you'd like

I would like some ways to get the `AuthenticationResult` and generate a response body from it. I suppose I could do that from the `IAuthorizationMiddlewareResultHandler` assuming I had access to the result, regardless of the authentication state. I would throw from the resultHandler which would be translated by the exception handler.

Or am I missing something here ? Any Documentation to point me to ?

### Additional context

_No response_

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.