<legend id='VJUmD'><style id='VJUmD'><dir id='VJUmD'><q id='VJUmD'></q></dir></style></legend>
      1. <tfoot id='VJUmD'></tfoot>
        • <bdo id='VJUmD'></bdo><ul id='VJUmD'></ul>

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

      4. JwtSecurityTokenHandler 和 TokenValidationParameters

        时间:2023-06-01
        • <bdo id='MPXOt'></bdo><ul id='MPXOt'></ul>

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

              <tbody id='MPXOt'></tbody>
            • <small id='MPXOt'></small><noframes id='MPXOt'>

              <tfoot id='MPXOt'></tfoot>

                1. 本文介绍了JwtSecurityTokenHandler 和 TokenValidationParameters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我曾经引用过 Microsoft.IdentityModel.Tokens.JWT 并且一切正常.

                  I used to have a reference to Microsoft.IdentityModel.Tokens.JWT and everything was working fine.

                  我更新为使用新的 System.IdentityModel.Tokens.Jwt 但现在似乎没有任何效果.找不到JwtSecurityTokenHandlerValidateToken方法,TokenValidationParameters没有AllowedAudienceSigningTokenValidateExpiration 属性.

                  I updated to use the new System.IdentityModel.Tokens.Jwt but nothing seems to work now. It cannot find the ValidateToken method of the JwtSecurityTokenHandler and the TokenValidationParameters have no AllowedAudience, SigningToken or ValidateExpiration properties.

                  我在这里缺少什么?任何人都可以提供一个 JWT 验证的工作示例吗?

                  What am I missing here? Can anyone provide with a working sample of a JWT validation with this?

                  我的旧"代码:

                  private static void ValidateJwt(string jwt)
                  {
                      var handler = new JWTSecurityTokenHandler();
                      var validationParameters = new Microsoft.IdentityModel.Tokens.JWT.TokenValidationParameters()
                      {
                          AllowedAudience = "https://my-rp.com",
                          //SigningToken = new BinarySecretSecurityToken(Convert.FromBase64String(myBase64Key)),
                          SigningToken = new X509SecurityToken(
                             X509
                             .LocalMachine
                             .My
                             .Thumbprint
                             .Find("UYTUYTVV99999999999YTYYTYTY88888888", false)
                             .First()),
                          ValidIssuer = "https://my-issuer.com/trust/issuer",
                          ValidateExpiration = true
                      };
                  
                      try
                      {
                          var principal = handler.ValidateToken(jwt, validationParameters);
                      }
                      catch (Exception e)
                      {
                  
                          Console.WriteLine("{0}
                   {1}", e.Message, e.StackTrace);
                      }
                  
                      Console.WriteLine();
                  }
                  

                  推荐答案

                  经过大量研究和测试,我终于发现TokenValidationParameters的一些属性名称发生了变化,JwtSecurityTokenHandler.ValidateToken() 方法签名.

                  After a lot of research and tests, I finally found that some properties names for TokenValidationParameters had changed and JwtSecurityTokenHandler.ValidateToken() method signature too.

                  所以这是上面代码的修改后的工作版本.

                  So here's the modified working version of the above code.

                  private static void ValidateJwt(string jwt)
                  {
                      var handler = new JwtSecurityTokenHandler();   
                      var validationParameters = new TokenValidationParameters()
                      {
                          ValidAudience = "https://my-rp.com",
                          IssuerSigningTokens = new List<X509SecurityToken>() { new X509SecurityToken(
                             X509
                             .LocalMachine
                             .My
                             .Thumbprint
                             .Find("UYTUYTVV99999999999YTYYTYTY88888888", false)
                             .First()) },
                          ValidIssuer = "https://my-issuer.com/trust/issuer",
                          CertificateValidator = X509CertificateValidator.None,
                          RequireExpirationTime = true
                      };
                  
                      try
                      {
                          SecurityToken validatedToken;
                          var principal = handler.ValidateToken(jwt, validationParameters, out validatedToken);
                      }
                      catch (Exception e)
                      {
                  
                          Console.WriteLine("{0}
                   {1}", e.Message, e.StackTrace);
                      }
                  
                      Console.WriteLine();
                  }
                  

                  作为参考,JwtSecurityTokenHandler 位于 System.IdentityModel.Tokens 命名空间中.不要忘记为 Microsoft .Net 添加包JSON Web 令牌处理程序Framework 4.5"(我写这些行时的版本 4.0.0).

                  And for the reference, the JwtSecurityTokenHandler lives in the System.IdentityModel.Tokens namespace. Don't forget to add the package "JSON Web Token Handler For the Microsoft .Net Framework 4.5" (version 4.0.0 at the time I write theses lines).

                  希望它可以为你们中的一些人节省几个小时的搜索时间!

                  Hope it can save a few hours of search for some of you guys!

                  这篇关于JwtSecurityTokenHandler 和 TokenValidationParameters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:IDX10603:算法:“HS256"要求 SecurityKey.KeySize 大于 下一篇:.NetCore JwtBearerAuthentication 不拒绝过期令牌

                  相关文章

                  最新文章

                2. <tfoot id='WWaBM'></tfoot>

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

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

                    • <bdo id='WWaBM'></bdo><ul id='WWaBM'></ul>

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