示例#1
0
        /// <summary>
        /// 颁发JWT字符串
        /// </summary>
        /// <param name="tokenModel"></param>
        /// <returns></returns>
        public static string IssueJWT(TokenModelJWT 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 claims = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(JwtRegisteredClaimNames.Iat, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),
                new Claim(JwtRegisteredClaimNames.Nbf, $"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),
                //过期时间,目前是过期100秒,可以自定义,注意Jwt有自己的缓冲过期时间
                new Claim(JwtRegisteredClaimNames.Exp, $"{new DateTimeOffset(DateTime.Now.AddHours(2)).ToUnixTimeSeconds()}"),
                new Claim(JwtRegisteredClaimNames.Iss, "YBlog.Core"),
                new Claim(JwtRegisteredClaimNames.Aud, "wr"),
                //这个Role是官方UseAuthentication要验证的Role,我们不用手动设置这个Role的属性了
                new Claim(ClaimTypes.Role, tokenModel.Role),
            };

            //秘钥
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JwtHelper.secretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

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

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

            return(encodedJwt);
        }
示例#2
0
        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="jwtStr"></param>
        /// <returns></returns>
        public static TokenModelJWT 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 TokenModelJWT
            {
                Uid  = (jwtToken.Id).ObjToInt(),
                Role = role != null?role.ObjToString() : "",
            };

            return(tm);
        }
        public async Task Invoke(HttpContext context)
        {
            if (!context.Request.Headers.ContainsKey("Authorization"))
            {
                await _next.Invoke(context);
            }
            else
            {
                var tokenHeader = context.Request.Headers["Authorization"].ToString();

                TokenModelJWT 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);
                context.User = principal;

                await _next.Invoke(context);
            }
        }