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

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

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

      如何验证 JWT 令牌

      时间:2023-06-03
        <tfoot id='vmBYb'></tfoot>

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

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

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

                本文介绍了如何验证 JWT 令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在尝试使用 JWT 令牌.我设法生成了一个有效的 JWTTokenString 并在 JWT 调试器 上对其进行了验证,但我有在 .Net 中验证令牌是不可能的.这是我到目前为止的代码:

                I'm trying to use JWT tokens. I managed to generate a valid JWTTokenString and validated it on the JWT debugger but I'm having an impossible time validating the token in .Net. Here's the code I have so far:

                class Program {
                
                    static string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
                
                    static void Main(string[] args) {
                        var stringToken = GenerateToken();
                        ValidateToken(stringToken);
                    }
                
                    private static string GenerateToken() {
                        var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
                
                        var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
                
                        var header = new JwtHeader(credentials);
                
                        var payload = new JwtPayload {
                           { "some ", "hello "},
                           { "scope", "world"},
                        };
                
                        var secToken = new JwtSecurityToken(header, payload);
                        var handler = new JwtSecurityTokenHandler();
                
                        return handler.WriteToken(secToken);
                
                    }
                
                    private static bool ValidateToken(string authToken) {
                        var tokenHandler = new JwtSecurityTokenHandler();
                        var validationParameters = GetValidationParameters();
                
                        SecurityToken validatedToken;
                        IPrincipal principal = tokenHandler.ValidateToken(authToken, validationParameters, out validatedToken);
                        Thread.CurrentPrincipal = principal;
                        return true;
                    }
                
                    private static TokenValidationParameters GetValidationParameters() {
                        return new TokenValidationParameters() {
                            //NOT A CLUE WHAT TO PLACE HERE
                        };
                    }
                }
                

                我想要的只是一个接收令牌并根据其有效性返回真或假的函数.从研究中我看到人们使用 IssuerSigningToken 来分配验证密钥.但是当我尝试使用它时,它似乎不存在.谁能帮我验证令牌?

                All I want is a function that receives a token and returns true or false based on its validity. From research I've seen people use IssuerSigningToken to assign the validation key. But when I try to use it, it doesn't seem to exist. Could anyone give me a hand on validating the token?

                推荐答案

                您必须使用与生成令牌相同的密钥来验证令牌.您还需要禁用一些验证,例如过期、颁发者和听众,因为您生成的令牌没有这些信息(或者您可以添加这些信息).这是一个工作示例:

                You must use the same key to validate the token as the one you use to generate it. Also you need to disable some validations such as expiration, issuer and audiance, because the token you generate doesn't have these information (or you can add these information). Here's a working example:

                class Program
                {
                    static string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
                
                    static void Main(string[] args)
                    {
                        var stringToken = GenerateToken();
                        ValidateToken(stringToken);
                    }
                
                    private static string GenerateToken()
                    {
                        var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
                        var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
                
                        var secToken = new JwtSecurityToken(
                            signingCredentials: credentials,
                            issuer: "Sample",
                            audience: "Sample",
                            claims: new[]
                            {
                                new Claim(JwtRegisteredClaimNames.Sub, "meziantou")
                            },
                            expires: DateTime.UtcNow.AddDays(1));
                
                        var handler = new JwtSecurityTokenHandler();
                        return handler.WriteToken(secToken);
                    }
                
                    private static bool ValidateToken(string authToken)
                    {
                        var tokenHandler = new JwtSecurityTokenHandler();
                        var validationParameters = GetValidationParameters();
                
                        SecurityToken validatedToken;
                        IPrincipal principal = tokenHandler.ValidateToken(authToken, validationParameters, out validatedToken);
                        return true;
                    }
                
                    private static TokenValidationParameters GetValidationParameters()
                    {
                        return new TokenValidationParameters()
                        {
                            ValidateLifetime = false, // Because there is no expiration in the generated token
                            ValidateAudience = false, // Because there is no audiance in the generated token
                            ValidateIssuer = false,   // Because there is no issuer in the generated token
                            ValidIssuer = "Sample",
                            ValidAudience = "Sample",
                            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key)) // The same key as the one that generate the token
                        };
                    }
                }
                

                这篇关于如何验证 JWT 令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 Jwt-Dotnet 生成有效令牌 下一篇:如何加密 JWT 安全令牌?

                相关文章

                最新文章

              1. <tfoot id='q5k24'></tfoot>

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

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

                2. <small id='q5k24'></small><noframes id='q5k24'>

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