systempuntoout
systempuntoout

Reputation: 74094

How do you get the Oauth2 AccessToken after its creation from the Owin Api?

If for some auditing reason I would be asked to store the AccessToken to Db after its creation, is there an Owin API Class that returns the AccessToken ?

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    //check user credentials
    ....
    context.Validated(ticket);
    //Where should I get the generated token?
    }
}

The only workaround I found is to create an Http filter in the Global.Asax that reads the output stream and get the Token from there.

Is there a more elegant way to get it directly from the Owin Api?

Upvotes: 9

Views: 4180

Answers (1)

Taiseer Joudeh
Taiseer Joudeh

Reputation: 9043

Sure you can but you need to use Microsoft.Owin.Security.OAuth version 3.0 not 2.1 and then override TokenEndpointResponse in class OAuthAuthorizationServerProvider as the code below

public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
    {
        var accessToken = context.AccessToken;
        return Task.FromResult<object>(null);
    }

Upvotes: 12

Related Questions