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

      <legend id='4nxLn'><style id='4nxLn'><dir id='4nxLn'><q id='4nxLn'></q></dir></style></legend>

    1. <small id='4nxLn'></small><noframes id='4nxLn'>

        <bdo id='4nxLn'></bdo><ul id='4nxLn'></ul>

        在 C# 中正确使用 JwtTokens

        时间:2023-06-03

            <tbody id='Hh2ef'></tbody>

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

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

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

                  本文介绍了在 C# 中正确使用 JwtTokens的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 JwtTokens 并且无法使它们正常工作.我正在使用 http://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/ 为它.我知道代码是一团糟,但只是为了展示我正在尝试做的事情.问题是我希望 JwtTokenHandler 因为生命周期而无法通过验证.

                  I'm playing a with JwtTokens and can't make them work properly. I'm using http://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/ for it. I know the code is a mess but is just to show what I'm trying to do. The problem is that I want the JwtTokenHandler to fail the validation because of the lifetime.

                  var key = "5A0AB091-3F84-4EC4-B227-0834FCD8B1B4";
                  var domain = "http://localhost";
                  var allowedAudience = "http://localhost";
                  var signatureAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
                  var digestAlgorithm = "http://www.w3.org/2001/04/xmlenc#sha256";
                  var issuer = "self";
                  var securityKey = System.Text.Encoding.Unicode.GetBytes(key);
                  var inMemorySymmetricSecurityKey = new InMemorySymmetricSecurityKey(securityKey);
                  
                  var now = DateTime.UtcNow;
                  var expiry = now.AddSeconds(1);
                  var tokenHandler = new JwtSecurityTokenHandler();
                  var claimsList = new List<Claim>()
                  {
                      new Claim(ClaimTypes.Name, "user"),
                      new Claim(ClaimTypes.Webpage, allowedAudience),
                      new Claim(ClaimTypes.Uri, domain),                
                      new Claim(ClaimTypes.Expiration,expiry.Ticks.ToString())
                  };
                  var roles = new List<string>() { "admin" };
                  claimsList.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role)));
                  
                  var identity = new GenericIdentity("user");
                  
                  var tokenDescriptor = new SecurityTokenDescriptor
                  {
                      Subject = new ClaimsIdentity(identity, claimsList),
                      TokenIssuerName = issuer,
                      AppliesToAddress = allowedAudience,
                      Lifetime = new Lifetime(now, expiry),
                      SigningCredentials = new SigningCredentials(inMemorySymmetricSecurityKey, signatureAlgorithm, digestAlgorithm),
                  };
                  
                  var token = tokenHandler.WriteToken(tokenHandler.CreateToken(tokenDescriptor));
                  
                  var validationParameters = new TokenValidationParameters()
                  {
                      ValidIssuer = issuer,
                      ValidAudience = allowedAudience,
                      IssuerSigningToken = new BinarySecretSecurityToken(securityKey)
                  };
                  
                  Thread.Sleep(2000);
                  try
                  {
                      SecurityToken securityToken;
                      tokenHandler.ValidateToken(token, validationParameters, out securityToken);
                      Console.WriteLine("OK");
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine("Error {0}", e.Message);
                  }
                  

                  由于我等待 2 秒,这不是假设会失败吗?如果我将 ValidationTokenParameter 的颁发者更改为x",它会失败...

                  Isn't this suppose to fail since I'm waiting 2 seconds? It fails if I change the issuer of the ValidationTokenParameter to "x"...

                  推荐答案

                  发现问题.验证参数的默认时钟偏差为 5 分钟

                  Found the issue. The validation parameters have a default clock skew of 5 minutes

                  /// <summary>
                  /// Default for the clock skew.
                  /// 
                  /// </summary>
                  /// 
                  /// <remarks>
                  /// 300 seconds (5 minutes).
                  /// </remarks>
                  public static readonly TimeSpan DefaultClockSkew;
                  

                  将其设置为 0 使这项工作.仍然不明白为什么倾斜是 5 分钟,如果我将到期时间设置在某个时间点!!!

                  Setting that to 0 make this work. Still don't understand why the skew is 5 minutes, if I set the expiry at some point!!!

                  这篇关于在 C# 中正确使用 JwtTokens的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JWT 错误 IDX10634:无法创建 SignatureProvider C# 下一篇:自己发行 JWT 令牌与使用 IdentityServer4(OIDC) 进行

                  相关文章

                  最新文章

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

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

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