如何重命名Web Api Bearer Token" .expires"键?

时间:2016-09-17 00:24:13

标签: asp.net-web-api jwt bearer-token

来自/ Token的响应返回如下所示的有效负载:

"access_token":"foo",
"token_type":"bearer",
"expires_in":59,
"refresh_token":"bar",
".issued":"Sat, 17 Sep 2016 00:13:21 GMT",
".expires":"Sat, 17 Sep 2016 00:14:21 GMT"

是否有理由.issued和.expired以他们的方式命名?这些不是有效的JavaScript属性,所以我打算重命名它们。如果有更优雅的方法来执行此操作,除了覆盖TokenEndpoint方法,如下所示:

public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
    foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
    {
        string key = property.Key;
        switch (key)
        {
            case ".expires":
                key = "expires";
                break;
            case ".issued":
                key = "issued";
                break;
        }
        context.AdditionalResponseParameters.Add(key, property.Value);
    }
    return Task.FromResult<object>(null);
}

我想要这样做的一个例子。我想提供一个TypeScript接口,模仿我期望从请求中接收的数据。在这种情况下,我的界面如下所示:

interface IToken {
    .expires: string;        // Not valid TypeScript
    .issued: string;         // Not valid TypeScript
    access_token: string;
    expires_in: number;
    token_type: string;
    refresh_token: string;
}

在不更改我的API中的属性的情况下,我和其他使用我的API的人都被迫使用像tokenVar[".expires"]这样的魔术字符串来访问这些属性

1 个答案:

答案 0 :(得分:1)

我认为没有具体的理由这样做。这只是另一个使用的常量。请参阅此处的AuthenticationProperties.cs https://github.com/jchannon/katanaproject/blob/master/src/Microsoft.Owin/Security/AuthenticationProperties.cs

正如您的代码覆盖TokenEndpoint OAuthAuthorizationServerProvider方法一样,这是最好的方法。

相关问题