aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Write custom authentication failure information to the response body

Open AlexandreBossard opened this issue 3 years ago • 0 comments

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<IAuthenticateResultFeature>().

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

AlexandreBossard avatar Sep 20 '22 22:09 AlexandreBossard