更改OWIN .expiration和.issued日期的DateTime格式

时间:2015-08-14 16:07:58

标签: c# .net asp.net-web-api oauth-2.0 owin

在创建.NET Web API启动程序模板时,我开始使用“个人用户帐户”身份验证选项(如here所述)。正确生成令牌,但“.issued”和“.expires”属性采用非ISO日期格式。如何使用DateTime.UtcNow.ToString("o")对其进行格式化,以使其符合ISO 8601标准?

{
  "access_token": "xxx",
  "token_type": "bearer",
  "expires_in": 1199,
  "userName": "foo@bar.com",
  "Id": "55ab2c33-6c44-4181-a24f-2b1ce044d981",
  ".issued": "Thu, 13 Aug 2015 23:08:11 GMT",
  ".expires": "Thu, 13 Aug 2015 23:28:11 GMT"
}

模板使用自定义OAuthAuthorizationServerProvider并提供一个钩子来为传出令牌添加其他属性('Id'和'userName'是我的道具),但我看不到任何改变现有的方法属性。

我注意到在TokenEndpoint的覆盖中,我得到了一个OAuthTokenEndpointContext,其中包含带有.issued和.expired键的属性字典。但是,尝试更改这些值无效。

非常感谢。

1 个答案:

答案 0 :(得分:11)

AuthenticationProperties类在Microsoft.Owin.dll中的Microsoft.Owin.Security命名空间中定义。

IssuedUtc属性的setter执行以下操作(ExpiresUtc类似):

this._dictionary[".issued"] = value.Value.ToString("r", (IFormatProvider) CultureInfo.InvariantCulture);

如您所见,设置IssuedUtc字典的.issued字段时也设置了"r" format

您可以尝试在TokenEndPoint方法中执行以下操作:

foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
    if (property.Key == ".issued")
    {
        context.AdditionalResponseParameters.Add(property.Key, context.Properties.IssuedUtc.Value.ToString("o", (IFormatProvider) CultureInfo.InvariantCulture));
    }
    else if (property.Key == ".expires")
    {
        context.AdditionalResponseParameters.Add(property.Key, context.Properties.ExpiresUtc.Value.ToString("o", (IFormatProvider) CultureInfo.InvariantCulture));
    }
    else
    {
        context.AdditionalResponseParameters.Add(property.Key, property.Value);
    }
}

我希望它有所帮助。

相关问题