/// <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); }
public static JwtBuilder Id(this JwtBuilder builder, Guid id) => builder.Id(id.ToString());
public static JwtBuilder IssuedAt(this JwtBuilder builder, DateTime time) => builder.AddClaim(ClaimName.IssuedAt, UnixEpoch.GetSecondsSinceAsString(time));
public static JwtBuilder NotBefore(this JwtBuilder builder, DateTime time) => builder.AddClaim(ClaimName.NotBefore, UnixEpoch.GetSecondsSinceAsString(time));
public static JwtBuilder Audience(this JwtBuilder builder, string audience) => builder.AddClaim(ClaimName.Audience, audience);
/// <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);
public static JwtBuilder MiddleName(this JwtBuilder builder, string middleName) => builder.AddClaim(ClaimName.MiddleName, middleName);
public static JwtBuilder GivenName(this JwtBuilder builder, string name) => builder.AddClaim(ClaimName.GivenName, name);
public static JwtBuilder IssuedAt(this JwtBuilder builder, long time) => builder.AddClaim(ClaimName.IssuedAt, time);
public static JwtBuilder NotBefore(this JwtBuilder builder, long time) => builder.AddClaim(ClaimName.NotBefore, time);
public static JwtBuilder ExpirationTime(this JwtBuilder builder, long time) => builder.AddClaim(ClaimName.ExpirationTime, time);
/// <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));
/// <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);
/// <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);
public static JwtBuilder Id(this JwtBuilder builder, long id) => builder.Id(id.ToString(CultureInfo.InvariantCulture));
public static JwtBuilder Id(this JwtBuilder builder, string id) => builder.AddClaim(ClaimName.JwtId, id);
public static JwtBuilder Issuer(this JwtBuilder builder, string issuer) => builder.AddClaim(ClaimName.Issuer, issuer);
public static JwtBuilder FamilyName(this JwtBuilder builder, string lastname) => builder.AddClaim(ClaimName.FamilyName, lastname);
public static JwtBuilder Subject(this JwtBuilder builder, string subject) => builder.AddClaim(ClaimName.Subject, subject);
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));