Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Bearer token JSON result in ASP.NET WebApi 2

I walkthrough this official ASP.NET tutorial, and the bearer token is published as below JSON.

{
    "access_token":"boQtj0SCGz2GFGz[...]",
    "token_type":"bearer",
    "expires_in":1209599,
    "userName":"Alice",
    ".issued":"Mon, 14 Oct 2013 06:53:32 GMT",
    ".expires":"Mon, 28 Oct 2013 06:53:32 GMT"
}

I'd like to add user profile properties along with the above result in order to reduce number of requests from clients. The example is as below...

{
    "access_token":"boQtj0SCGz2GFGz[...]",
    "token_type":"bearer",
    "expires_in":1209599,
    "userName":"Alice",
    ".issued":"Mon, 14 Oct 2013 06:53:32 GMT",
    ".expires":"Mon, 28 Oct 2013 06:53:32 GMT",
    "Notifications":35,
    "IsEventMember":true,
    "Promotion":324372
}

The oauth provider I use is from default ASP.NET template (ApplicationOAuthProvider.cs) and OAuthOption is as below.

            OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
            AllowInsecureHttp = true
        };

How can I make it?

Note that my question is different from adding extra claims.

like image 533
Youngjae Avatar asked Jun 20 '14 07:06

Youngjae


1 Answers

Well here is the solution:

public override async Task TokenEndpoint(OAuthTokenEndpointContext context)
{
    context.AdditionalResponseParameters.Add("username", "[email protected]");

    return Task.FromResult<object>(null);
}
like image 192
JuChom Avatar answered Oct 24 '22 03:10

JuChom