Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public Task Invoke(HttpContext httpContext)
        {
            //检测是否包含'Authorization'请求头
            if (httpContext.Request.Headers.ContainsKey("Authorization"))
            {
                var tokenHeader = httpContext.Request.Headers["Authorization"].ToString();

                JwtTokenModel tm = JwtHelper.SerializeJWT(tokenHeader);                //序列化token,获取授权

                //授权 注意这个可以添加多个角色声明,请注意这是一个 list
                var claimList = new List <Claim>();
                var claim     = new Claim(ClaimTypes.Role, tm.Role);
                claimList.Add(claim);
                var identity  = new ClaimsIdentity(claimList);
                var principal = new ClaimsPrincipal(identity);
                httpContext.User = principal;
            }
            return(_next(httpContext));
        }
Example #2
0
        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="jwtStr"></param>
        /// <returns></returns>
        public static JwtTokenModel SerializeJWT(string jwtStr)
        {
            var jwtHandler            = new JwtSecurityTokenHandler();
            JwtSecurityToken jwtToken = jwtHandler.ReadJwtToken(jwtStr);
            object           role     = new object();;

            try
            {
                jwtToken.Payload.TryGetValue("Role", out role);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            var tm = new JwtTokenModel
            {
                Uid  = Convert.ToInt32(jwtToken.Id),
                Role = role?.ToString() ?? "",
            };

            return(tm);
        }
Example #3
0
        /// <summary>
        /// 颁发JWT字符串
        /// </summary>
        /// <param name="tokenModel"></param>
        /// <returns></returns>
        public static string IssueJWT(JwtTokenModel tokenModel)
        {
            var dateTime = DateTime.UtcNow;
            var claims   = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Jti, tokenModel.Uid.ToString()), //Id
                new Claim("Role", tokenModel.Role),                                //角色
                new Claim(JwtRegisteredClaimNames.Iat, dateTime.ToString(), ClaimValueTypes.Integer64)
            };
            //秘钥
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var jwt = new JwtSecurityToken(
                issuer: "Demo.Core",
                claims: claims,                 //声明集合
                expires: dateTime.AddHours(2),
                signingCredentials: creds);

            var jwtHandler = new JwtSecurityTokenHandler();
            var encodedJwt = jwtHandler.WriteToken(jwt);

            return(encodedJwt);
        }