2015-08-14 101 views
4

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

{ 
    "access_token": "xxx", 
    "token_type": "bearer", 
    "expires_in": 1199, 
    "userName": "[email protected]", 
    "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”和“用户名”是我的道具),但我看不出有什么办法改变现有的属性。

我注意到在覆盖TokenEndpoint时,我得到一个OAuthTokenEndpointContext,它有一个带有.issued和.expired键的属性字典。然而,试图改变这些值并没有效果。

非常感谢。

+0

如果时间未通过ISO格式,您是否遇到任何问题?我认为你需要客户JSON.Net序列化来默认序列化日期类型为ISO – harishr

回答

6

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); 
    } 
} 

我希望它能帮助。

+0

钉住它!谢谢! – tobyb

+0

令人难以置信的是,MS不会以每个人都能理解的格式发送日期!特别是与身份验证一样重要的事情! –

+0

@MattSkeldon你的意思是像RFC1123? – Mardoxx