jwt Token验证与解析
Posted anang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jwt Token验证与解析相关的知识,希望对你有一定的参考价值。
网上似乎没有相关代码
贴上一段Token的解析认证
[TestMethod] public void TestMethod1() { string Token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJMb2dpblR5cGUiOiLlvq7kv6HnmbvlvZUiLCJJc09uZSI6IkZhbHNlIiwiSUQiOiIxMDc2NDYyODY4OTQzMjEyNTQzIiwiUGhvbmUiOiIxMSIsIkVtYWlsIjoiMzMyMSIsIk9wZW5pZCI6IkJCIiwiTmFtZSI6IjEyMyIsIm5iZiI6MTU0Njc1MDQxOSwiZXhwIjoxNTQ2NzUyMjE5LCJpc3MiOiJUZXN0SXNzdWVyIiwiYXVkIjoiVGVzdEF1ZGllbmNlIn0._g3rfr2P3YEFRgKlxv_7DtnxkLq4CrfXBdEUHUDl9jc"; /// 密匙 string IssuerSigningKey = "AAAAAAAAAAAAAAAABBBBBBBBBVCCCCC=="; /// 发行 string ValidIssuer = "TestIssuer"; /// 观众 string ValidAudience = "TestAudience"; var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(IssuerSigningKey)); var tokenValidationParams = new TokenValidationParameters() { ValidateLifetime = true, ValidateAudience = true, ValidateIssuer = true, ValidateIssuerSigningKey = true, ValidIssuer = ValidIssuer, ValidAudience= ValidAudience, IssuerSigningKey = secretKey, }; var jwtTokenHandler = new JwtSecurityTokenHandler(); var a = jwtTokenHandler.ValidateToken(Token, tokenValidationParams, out SecurityToken validated); }
效果:
以上是关于jwt Token验证与解析的主要内容,如果未能解决你的问题,请参考以下文章