In my AuthenticationHandler I have some (shared) code like:
try
{
doSomething()
}
catch(AuthException e)
{
return AuthenticateResult.Fail(e);
}
I would like to be able to access that exception in other middleware. something like :
public async Task Invoke(HttpContext context)
{
context. ??? AuthenticationContext.Exception?
}
I know this is somewhat old question, but to solve this, I decided to use HttpContext.Items
in my implementation of IAuthenticationService
to put a value with key like AuthenticationException
there, and then read that in other middleware or filter.
Note that It may look tempting to use HttpContext.Features.Get<IAuthenticateResultFeature>().AuthenticateResult
, but this feature is not set for failed authentications! I've spent several hours debugging this, only to understand that I need to look for another way.