Cannot raise context.SetError from IAuthenticationTokenProvider

I have a simple OAuth 2.0 implementation from OWIN in my application that supports update tokens.

void Receive(AuthenticationTokenReceiveContext context)

In this method, if the update token is invalid, I want to call SetError (or something similar) to return the correct json response instead of the default value "invalid_grant", the context that has the type AuthenticationTokenReceiveContextdoes not seem to contain any such method. while I just set an empty ticket and in my method GrantRefreshTokenI check if the ticket contains any claims, if this does not mean that the update token is invalid, and I set the error response from there like this

public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
{
    return Task.Run(() =>
    {
        var claimCount = context.Ticket.Identity.Claims.Count();

        if (claimCount > 0)
            context.Validated(context.Ticket.Identity);
        else
            RejectWithJson(context, new GeneralError { ErrorCode = GeneralErrorCode.InvalidRefreshToken, Message = "Invalid refresh token" }, HttpStatusCode.Unauthorized);
    });
}

Is there a way to do this from a method Receive?

+4

All Articles