在ASP.NET WebApi 2中自定义承载令牌JSON结果

You*_*jae 7 c# json access-token asp.net-web-api asp.net-identity

我通过这个官方的ASP.NET教程,并且持有者令牌发布如下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"
}
Run Code Online (Sandbox Code Playgroud)

我想添加用户配置文件属性以及上面的结果,以减少来自客户端的请求数.示例如下......

{
    "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
}
Run Code Online (Sandbox Code Playgroud)

我使用的oauth提供程序来自默认的ASP.NET模板(ApplicationOAuthProvider.cs),OAuthOption如下所示.

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

我该怎么做?

请注意,我的问题与添加额外声明不同.

Swe*_*ell 11

那么这是解决方案:

public override async Task TokenEndpoint(OAuthTokenEndpointContext context)
{
    context.AdditionalResponseParameters.Add("username", "user@mail.com");

    return Task.FromResult<object>(null);
}
Run Code Online (Sandbox Code Playgroud)