Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 997
dgn Avatar asked Jun 06 '14 09:06

dgn


2 Answers

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);
}
like image 68
jd4u Avatar answered Nov 18 '22 21:11

jd4u


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);
    }
like image 2
Behzad Bahmanyar Avatar answered Nov 18 '22 20:11

Behzad Bahmanyar