其他分享
首页 > 其他分享> > 解析jwt

解析jwt

作者:互联网

            var path = context.HttpContext.Request.Path.Value;
            var tokenHeader = context.HttpContext.Request.Headers["Authorization"];

            var strToken = tokenHeader.ToString();
            if (strToken.Contains("Bearer "))
            {
                var jwtHandler = new JwtSecurityTokenHandler();
                try
                {
                    JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(strToken.Remove(0, 7)); //去除"Bearer "
                    var identity = new ClaimsIdentity(jwtToken.Claims);
                    var principal = new ClaimsPrincipal(identity);
                    context.HttpContext.User = principal;
                }
                catch
                {
                    _logger.Info(new Sino.Web.Logging.LogInfo() { Method = path, Argument = strToken, Description = "鉴权失败" });
                    throw new SinoException(ErrorCode.E100003, nameof(ErrorCode.E100003).GetCode());
                }
            }

 

标签:jwt,jwtHandler,strToken,context,var,new,HttpContext,解析
来源: https://www.cnblogs.com/yiyanwei/p/15697832.html