예제 #1
0
파일: JwtToken.cs 프로젝트: ztxyzu/BasisSoa
        /// <summary>
        /// 解析JWT
        /// </summary>
        /// <param name="jwtStr"></param>
        /// <returns></returns>
        public static TokenModelBeta SerializeJWT(string jwtStr)
        {
            var jwtHandler             = new JwtSecurityTokenHandler();
            JwtSecurityToken jwtToken  = jwtHandler.ReadJwtToken(jwtStr);
            object           role      = new object();
            object           organize  = new object();
            object           name      = new object();
            object           tokentype = new object();
            object           isAdmin   = new object();

            try
            {
                jwtToken.Payload.TryGetValue(ClaimTypes.GroupSid, out organize);
                jwtToken.Payload.TryGetValue(ClaimTypes.Role, out role);
                jwtToken.Payload.TryGetValue(ClaimTypes.Name, out name);
                jwtToken.Payload.TryGetValue(ClaimTypes.Gender, out tokentype);
                jwtToken.Payload.TryGetValue(ClaimTypes.Authentication, out isAdmin);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            var tm = new TokenModelBeta
            {
                Id        = jwtToken.Id,
                Role      = role.ToString(),
                Organize  = organize.ToString(),
                Name      = name.ToString(),
                TokenType = tokentype.ToString(),
                IsAdmin   = isAdmin.ToString() == "1" ? true : false
            };

            return(tm);
        }
예제 #2
0
파일: JwtToken.cs 프로젝트: ztxyzu/BasisSoa
        /// <summary>
        /// 授权解析jwt
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public static TokenModelBeta ParsingJwtToken(HttpContext httpContext)
        {
            if (!httpContext.Request.Headers.ContainsKey("Authorization"))
            {
                return(null);
            }
            var            tokenHeader = httpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            TokenModelBeta tm          = SerializeJWT(tokenHeader);

            return(tm);
        }