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

tob*_*byb 6 .net c# oauth-2.0 asp.net-web-api owin

我开始与"个人用户帐户"身份验证选项创建的.NET Web API启动模板时(如描述在这里).正确生成令牌,但".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"
}
Run Code Online (Sandbox Code Playgroud)

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

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

非常感谢提前.

jum*_*uro 11

AuthenticationPropertiesclass Microsoft.Owin.Security在Microsoft.Owin.dll 中的命名空间中定义.

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

this._dictionary[".issued"] = value.Value.ToString("r", (IFormatProvider) CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,设置在IssuedUtc.issued辞典的现场设置过,并与"R"格式.

您可以尝试在该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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助.

  • @MattSkeldon你的意思是像RFC1123? (2认同)