Exemple #1
0
        /// <summary>
        /// Jwt解密
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static TokenModelJwt JwtDecrypt(string tokenStr)
        {
            if (string.IsNullOrEmpty(tokenStr) || string.IsNullOrWhiteSpace(tokenStr))
            {
                return(new TokenModelJwt());
            }
            if (tokenStr.Substring(0, 6).ToLower() == "bearer")
            {
                tokenStr = tokenStr.Substring(7);//截取前面的bearer和空格
            }
            var jwtHandler         = new JwtSecurityTokenHandler();
            JwtSecurityToken token = jwtHandler.ReadJwtToken(tokenStr);

            token.Payload.TryGetValue(ClaimTypes.Role, out object level);
            var model = new TokenModelJwt
            {
                UserId = Guid.Parse(token.Id),
                Level  = level == null ? "" : level.ToString()
            };

            return(model);
        }