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);

        }

 

效果:

 

posted @ 2019-01-06 13:02  AnAng  阅读(21062)  评论(3编辑  收藏  举报