【发布时间】:2021-06-05 17:19:07
【问题描述】:
我正在使用 C# OWIN 框架进行身份验证,并且我有 TokenEndpoint 覆盖方法,它可以将 IssuedDate 和 ExpiresDate 转换为正确的格式,所以我使用 DateTimeOffset.TryParse 方法来检查和返回日期时间,所以现在我的问题是m 面真的很奇怪,其中一个服务正常工作,为方法 DateTimeOffset.TryParse 返回 true,而另一个服务返回 false,即使两者都传递相同的日期格式。早些时候我使用了 DateTimeOffset.Parse,它失败了,原因是“字符串未被识别为有效的日期时间”。所以我改为 DateTimeOffset.TryParse 来处理错误,但我仍然无法找到根本原因。
public override async Task TokenEndpoint(OAuthTokenEndpointContext context)
{
foreach (System.Collections.Generic.KeyValuePair<string, string> property in context.Properties.Dictionary)
{
DateTimeOffset result;
if ((property.Key.Equals(".issued") || property.Key.Equals(".expires")) && DateTimeOffset.TryParse(property.Value, out result))
{
context.AdditionalResponseParameters.Add(property.Key, result.UtcDateTime.ToString("yyyy/MM/dd HH:mm:ss"));
}
else
{
context.AdditionalResponseParameters.Add(property.Key, property.Value);
}
}
}
两种服务都以以下格式传递日期。 格林威治标准时间 2021 年 3 月 7 日星期日 11:31:14 格林威治标准时间 2021 年 3 月 7 日星期日 11:32:44
【问题讨论】:
-
之前我使用过 DateTimeOffset.Parse,但它失败了,原因是“字符串未被识别为有效的日期时间”。 - 你能告诉我们抛出的确切日期字符串吗那个错误?尝试追查那个字符串,因为这听起来像是根本原因
-
@haldo 我已在快照和我的问题“Sun, 07 Mar 2021 11:32:44 GMT”中附加了附件
标签: c# owin datetimeoffset tryparse