/// <summary>
        /// 创建token
        /// </summary>
        /// <param name="dic">用户信息</param>
        /// <returns></returns>
        public static string GenerateToken(Dictionary <string, object> dic, int timeout)
        {
            JWT.Builder.JwtBuilder b = new JWT.Builder.JwtBuilder();
            string secret            = ConfigHelper.GetConfigString("JWTSecret");

            if (string.IsNullOrEmpty(secret))
            {
                throw new Exception("Token密钥未设置!");
            }
            if (timeout == 0)
            {
                timeout = 60;
            }
            //用户信息
            var payload = dic;

            //过期时间
            dic.Add("exp", UnixTimeStampUTC(DateTime.UtcNow.AddMinutes(timeout)));
            IJwtAlgorithm     algorithm  = new HMACSHA256Algorithm();
            IJsonSerializer   serializer = new JsonNetSerializer();
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder       encoder    = new JwtEncoder(algorithm, serializer, urlEncoder);
            string            token      = encoder.Encode(payload, secret);

            return(token);
        }
Exemple #2
0
 public static JwtBuilder Id(this JwtBuilder builder, Guid id) =>
 builder.Id(id.ToString());
Exemple #3
0
 public static JwtBuilder IssuedAt(this JwtBuilder builder, DateTime time) =>
 builder.AddClaim(ClaimName.IssuedAt, UnixEpoch.GetSecondsSinceAsString(time));
Exemple #4
0
 public static JwtBuilder NotBefore(this JwtBuilder builder, DateTime time) =>
 builder.AddClaim(ClaimName.NotBefore, UnixEpoch.GetSecondsSinceAsString(time));
Exemple #5
0
 public static JwtBuilder Audience(this JwtBuilder builder, string audience) =>
 builder.AddClaim(ClaimName.Audience, audience);
Exemple #6
0
 /// <summary>
 /// Adds well-known claim to the JWT.
 /// </summary>
 public static JwtBuilder AddClaim(this JwtBuilder builder, ClaimName name, object value) =>
 builder.AddClaim(name.GetPublicClaimName(), value);
Exemple #7
0
 public static JwtBuilder MiddleName(this JwtBuilder builder, string middleName) =>
 builder.AddClaim(ClaimName.MiddleName, middleName);
Exemple #8
0
 public static JwtBuilder GivenName(this JwtBuilder builder, string name) =>
 builder.AddClaim(ClaimName.GivenName, name);
Exemple #9
0
 public static JwtBuilder IssuedAt(this JwtBuilder builder, long time) =>
 builder.AddClaim(ClaimName.IssuedAt, time);
Exemple #10
0
 public static JwtBuilder NotBefore(this JwtBuilder builder, long time) =>
 builder.AddClaim(ClaimName.NotBefore, time);
Exemple #11
0
 public static JwtBuilder ExpirationTime(this JwtBuilder builder, long time) =>
 builder.AddClaim(ClaimName.ExpirationTime, time);
Exemple #12
0
 /// <summary>
 /// Adds several claims to the JWT
 /// </summary>
 public static JwtBuilder AddClaims(this JwtBuilder builder, IDictionary <string, object> claims) =>
 claims.Aggregate(builder, (b, p) => b.AddClaim(p.Key, p.Value));
Exemple #13
0
 /// <summary>
 /// Adds well-known claim to the JWT.
 /// </summary>
 public static JwtBuilder AddClaim <T>(this JwtBuilder builder, string name, T value) =>
 builder.AddClaim(name, value);
Exemple #14
0
 /// <summary>
 /// Adds well-known claim to the JWT.
 /// </summary>
 public static JwtBuilder AddClaim <T>(this JwtBuilder builder, ClaimName name, T value) =>
 builder.AddClaim(name, (object)value);
Exemple #15
0
 public static JwtBuilder Id(this JwtBuilder builder, long id) =>
 builder.Id(id.ToString(CultureInfo.InvariantCulture));
Exemple #16
0
 public static JwtBuilder Id(this JwtBuilder builder, string id) =>
 builder.AddClaim(ClaimName.JwtId, id);
Exemple #17
0
 public static JwtBuilder Issuer(this JwtBuilder builder, string issuer) =>
 builder.AddClaim(ClaimName.Issuer, issuer);
Exemple #18
0
 public static JwtBuilder FamilyName(this JwtBuilder builder, string lastname) =>
 builder.AddClaim(ClaimName.FamilyName, lastname);
Exemple #19
0
 public static JwtBuilder Subject(this JwtBuilder builder, string subject) =>
 builder.AddClaim(ClaimName.Subject, subject);
Exemple #20
0
 public static JwtBuilder ExpirationTime(this JwtBuilder builder, DateTime time) =>
 builder.AddClaim(ClaimName.ExpirationTime, UnixEpoch.GetSecondsSinceAsString(time));
 /// <summary>
 /// Adds several claims to the JWT
 /// </summary>
 public static JwtBuilder AddClaims(this JwtBuilder builder, IEnumerable <KeyValuePair <string, object> > claims) =>
 claims.Aggregate(builder, (b, p) => b.AddClaim(p.Key, p.Value));