微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在创建.NET Web API启动程序模板时,我从“个人用户帐户”身份验证选项开始(如 here所述).正确生成令牌,但“.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并提供一个钩子来向传出令牌添加其他属性(‘Id’和’userName’是我的道具),但我没有看到任何方法来更改现有属性.

我注意到在覆盖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);
    }
}

我希望它有所帮助.

原文地址:https://www.jb51.cc/csharp/92099.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐