加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:18:41 所属栏目:百科 来源:网络整理
导读:在创建.NET Web API启动程序模板时,我从“个人用户帐户”身份验证选项开始(如 here所述).正确生成令牌,但“.issued”和“.expires”属性采用非ISO日期格式.如何使用 DateTime.UtcNow.ToString("o") 格式化它们以符合ISO 8601标准? { "access_token": "xxx",
在创建.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);
    }
}

我希望它有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读