dgn
dgn

Reputation: 1223

AspNet Identity 2: Customize OAuth endpoint response

I successfully implemented my custom OAuthAuthorizationServerProvider. But when I log in and retrieve a token, my client doesn't have any idea of the user's roles, claims, etc.

I currently added a webapi controller to return the list of the principal's claims, but I'm not really happy with that.

When requesting a token, the current response looks like:

{
    access_token: "qefelgrebjhzefilrgo4583535",
    token_type: "bearer",
    expires_in: 59
}

Q> How can make it return something like the following snippet?

{
    access_token: "qefelgrebjhzefilrgo4583535",
    token_type: "bearer",
    expires_in: 59,
    user: {
        name: 'foo',
        role: 'bar'
    }
}

My progress so far:

The documentation of OAuthAuthorizationServerProvider#TokenEndpoint(OAuthTokenEndpointContext) says:

Called at the final stage of a successful Token endpoint request. An application may implement this call in order to do any final modification of the claims being used to issue access or refresh tokens. This call may also be used in order to add additional response parameters to the Token endpoint's json response body.

I couldn't find any example of how to customize the response, and asp-net Identity's source code is not yet released, so I'm quite stuck.

Upvotes: 28

Views: 10647

Answers (2)

Behzad Bahmanyar
Behzad Bahmanyar

Reputation: 6295

I believe you need to override TokenEndpointResponse on OAuthAuthorizationServerProvider class :

    public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
    {
        context.AdditionalResponseParameters.Add("Key","Value");
        return base.TokenEndpointResponse(context);
    }

Upvotes: 2

jd4u
jd4u

Reputation: 5807

May be you are looking for TokenEndpoint method override of OAuthAuthorizationServerProvider.

public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
    foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
    {
        context.AdditionalResponseParameters.Add(property.Key, property.Value);
    }

    return Task.FromResult<object>(null);
}

Upvotes: 40

Related Questions