Exemple #1
0
        /// <summary>
        /// 颁发JWT字符串 /// </summary>
        /// <param name="tokenModel"></param>
        /// <returns></returns>
        public static string IssueJWT(TokenModel tokenModel)
        {
            var jwtConfig = new JwtAuthConfigModel();
            //过期时间(分钟)
            double exp = 0;

            switch (tokenModel.TokenType)
            {
            case "Web":
                exp = jwtConfig.WebExp;
                break;

            case "App":
                exp = jwtConfig.AppExp;
                break;

            case "Wx":
                exp = jwtConfig.WxExp;
                break;

            case "Other":
                exp = jwtConfig.OtherExp;
                break;
            }
            var dateTime = DateTime.UtcNow;
            var claims   = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Jti, tokenModel.Uid),
                new Claim("UserName", tokenModel.UserName),    //用户名
                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.AddMinutes(exp)).ToUnixTimeSeconds()}"),
                new Claim(JwtRegisteredClaimNames.Iss, jwtConfig.Issuer),
                new Claim(JwtRegisteredClaimNames.Aud, jwtConfig.Audience),
                new Claim(ClaimTypes.Role, tokenModel.Role),
            };
            //秘钥
            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConfig.JWTSecretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwt   = new JwtSecurityToken(
                issuer: jwtConfig.Issuer,
                audience: jwtConfig.Audience,
                claims: claims,
                expires: dateTime.AddMinutes(exp),
                signingCredentials: creds);
            var jwtHandler = new JwtSecurityTokenHandler();
            var encodedJwt = jwtHandler.WriteToken(jwt);

            return(encodedJwt);
        }
Exemple #2
0
        /// <summary>
        /// 颁发JWT字符串
        /// </summary>
        /// <param name="tokenModel"></param>
        /// <returns></returns>
        public static string IssueJWT(TokenModel tokenModel)
        {
            var dateTime = DateTime.UtcNow;
            var claims   = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Jti, tokenModel.Uid.ToString()), //用户Id
                new Claim("UserName", tokenModel.UserName),                        //用户名
                new Claim("Role", tokenModel.Role),                                //身份
                new Claim("Project", tokenModel.Project),                          //身份
                new Claim(JwtRegisteredClaimNames.Iat, dateTime.ToString(), ClaimValueTypes.Integer64)
            };
            //秘钥
            var jwtConfig = new JwtAuthConfigModel();
            var key       = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConfig.JWTSecretKey));
            var creds     = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            //过期时间
            double exp = 0;

            switch (tokenModel.TokenType)
            {
            case "Web":
                exp = jwtConfig.WebExp;
                break;

            case "App":
                exp = jwtConfig.AppExp;
                break;

            case "MiniProgram":
                exp = jwtConfig.MiniProgramExp;
                break;

            case "Other":
                exp = jwtConfig.OtherExp;
                break;
            }
            var jwt = new JwtSecurityToken(
                issuer: "FytSoa",
                claims: claims, //声明集合
                expires: dateTime.AddHours(exp),
                signingCredentials: creds);

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

            return(encodedJwt);
        }