<i id='S9ss9'><tr id='S9ss9'><dt id='S9ss9'><q id='S9ss9'><span id='S9ss9'><b id='S9ss9'><form id='S9ss9'><ins id='S9ss9'></ins><ul id='S9ss9'></ul><sub id='S9ss9'></sub></form><legend id='S9ss9'></legend><bdo id='S9ss9'><pre id='S9ss9'><center id='S9ss9'></center></pre></bdo></b><th id='S9ss9'></th></span></q></dt></tr></i><div id='S9ss9'><tfoot id='S9ss9'></tfoot><dl id='S9ss9'><fieldset id='S9ss9'></fieldset></dl></div>

  1. <small id='S9ss9'></small><noframes id='S9ss9'>

    <tfoot id='S9ss9'></tfoot>
      • <bdo id='S9ss9'></bdo><ul id='S9ss9'></ul>
      <legend id='S9ss9'><style id='S9ss9'><dir id='S9ss9'><q id='S9ss9'></q></dir></style></legend>

      System.IdentityModel.Tokens.JwtSecurityToken 自定义属性

      时间:2023-06-03

          <legend id='xXwQZ'><style id='xXwQZ'><dir id='xXwQZ'><q id='xXwQZ'></q></dir></style></legend>

            <tfoot id='xXwQZ'></tfoot>

            <small id='xXwQZ'></small><noframes id='xXwQZ'>

            • <bdo id='xXwQZ'></bdo><ul id='xXwQZ'></ul>
            • <i id='xXwQZ'><tr id='xXwQZ'><dt id='xXwQZ'><q id='xXwQZ'><span id='xXwQZ'><b id='xXwQZ'><form id='xXwQZ'><ins id='xXwQZ'></ins><ul id='xXwQZ'></ul><sub id='xXwQZ'></sub></form><legend id='xXwQZ'></legend><bdo id='xXwQZ'><pre id='xXwQZ'><center id='xXwQZ'></center></pre></bdo></b><th id='xXwQZ'></th></span></q></dt></tr></i><div id='xXwQZ'><tfoot id='xXwQZ'></tfoot><dl id='xXwQZ'><fieldset id='xXwQZ'></fieldset></dl></div>
                  <tbody id='xXwQZ'></tbody>

                本文介绍了System.IdentityModel.Tokens.JwtSecurityToken 自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我的 AuthServer 目前正在使用以下代码生成 JwtSecurityToken:

                My AuthServer is currently using the following code to generate a JwtSecurityToken:

                var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
                var handler = new JwtSecurityTokenHandler();
                var jwt = handler.WriteToken(token);
                

                有效载荷如下所示:

                {
                  "unique_name": "myUserName",
                  "sub": "myUserName",
                  "role": "API_User",
                  "iss": "Automation",
                  "aud": "099153c2625149bc8ecb3e85e03f0022",
                  "exp": 1486056731,
                  "nbf": 1483464731
                }
                

                我想在令牌负载中添加一些自定义字段/属性,例如 ProfilePicURL,以便负载看起来像这样:

                I would like to add some custom fields/properties within the token payload, such as ProfilePicURL, so that the payload can look something like this:

                {
                  "unique_name": "myUserName",
                  "sub": "myUserName",
                  "role": "API_User",
                  "iss": "Automation",
                  "aud": "099153c2625149bc8ecb3e85e03f0022",
                  "exp": 1486056731,
                  "nbf": 1483464731,
                  "profilePicture": "http://url/user.jpg"
                }
                

                如何添加这些自定义属性并确保令牌包含它们?

                How do I go about adding these custom properties and ensuring that the token contains them?

                推荐答案

                JwtSecurityToken 暴露了一个 JwtPayload Payload { get;设置;} 属性.JwtPayload 派生自 Dictionary 所以只需将其添加到有效负载中...

                JwtSecurityToken exposes a JwtPayload Payload { get; set;} property. JwtPayload derives from Dictionary<string, object> so just add it to the payload...

                var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
                token.Payload["profilePicture"] = "http://url/user.jpg"
                var handler = new JwtSecurityTokenHandler();
                var jwt = handler.WriteToken(token);
                

                使用 WriteToken 对令牌进行编码和签名很重要,因为简单地获取 RawData 属性将不起作用(令牌将不包含自定义声明).

                It is important that you use WriteToken to encode and sign the token, as simply getting the RawData property will not work (the token will not contain the custom claims).

                这篇关于System.IdentityModel.Tokens.JwtSecurityToken 自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:无法使用 .NET 验证 JSON Web 令牌 - 密钥太短 下一篇:JWT 如何添加自定义声明和解码声明

                相关文章

                最新文章

                  <bdo id='dQIIj'></bdo><ul id='dQIIj'></ul>

                  <small id='dQIIj'></small><noframes id='dQIIj'>

                  1. <legend id='dQIIj'><style id='dQIIj'><dir id='dQIIj'><q id='dQIIj'></q></dir></style></legend>

                  2. <i id='dQIIj'><tr id='dQIIj'><dt id='dQIIj'><q id='dQIIj'><span id='dQIIj'><b id='dQIIj'><form id='dQIIj'><ins id='dQIIj'></ins><ul id='dQIIj'></ul><sub id='dQIIj'></sub></form><legend id='dQIIj'></legend><bdo id='dQIIj'><pre id='dQIIj'><center id='dQIIj'></center></pre></bdo></b><th id='dQIIj'></th></span></q></dt></tr></i><div id='dQIIj'><tfoot id='dQIIj'></tfoot><dl id='dQIIj'><fieldset id='dQIIj'></fieldset></dl></div>
                  3. <tfoot id='dQIIj'></tfoot>