gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:08:25 26 4
gpt4 key购买 nike

在创建 .NET Web API 入门模板时,我从“个人用户帐户”身份验证选项开始(如 here 所述)。 token 已正确生成,但“.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 并提供一个钩子(Hook)来向传出 token 添加额外的属性('Id' 和 'userName' 是我的 Prop ),但我看不到任何方式更改现有属性。

我确实注意到在 TokenEndpoint 的覆盖中,我得到一个 OAuthTokenEndpointContext,它有一个带有 .issued 和 .expired 键的属性字典。然而,尝试更改这些值没有任何效果。

提前致谢。

最佳答案

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

希望对你有帮助。

关于c# - 更改 OWIN .expiration 和 .issued 日期的 DateTime 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32014315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com