2015-08-14 58 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