public static Dictionary <string, object> Decode(string jwtStr, string key = null) { if (string.IsNullOrWhiteSpace(key)) { key = Key; } try { IJsonSerializer jsonSerializer = new JsonNetSerializer(); IDateTimeProvider dateTimeProvider = new UtcDateTimeProvider(); IJwtValidator jwtValidator = new JwtValidator(jsonSerializer, dateTimeProvider); IAlgorithmFactory algorithmFactory = new HMACSHAAlgorithmFactory(); IJwtAlgorithm jwtAlgorithm = new HMACSHA256Algorithm(); IBase64UrlEncoder base64UrlEncoder = new JwtBase64UrlEncoder(); IJwtDecoder jwtDecoder = new JwtDecoder(jsonSerializer, jwtValidator, base64UrlEncoder, algorithmFactory); var json = jwtDecoder.Decode(token: jwtStr, key, verify: true); var result = JsonConvert.DeserializeObject <Dictionary <string, object> >(json); if (Convert.ToDateTime(result["timeout"]) < DateTime.Now) { throw new Exception(message: "token已过期请重新登录"); } else { result.Remove(key: "timeout"); } return(result); } catch (TokenExpiredException) { throw; } }
public TBody GetBody() { // 获取 密钥 string secret = SecretBuilder.Build(); if (string.IsNullOrWhiteSpace(secret)) { throw new Exception("应用程序密钥(AppSecret)为空或null"); } ICookieFactory cookieFactory = new CookieFactory(); ICookieClient cookieClient = cookieFactory.Create(); if (!cookieClient.Contains(AuthConfigProvider.AuthConfig.CookieName)) { return(null); } // 获取cookie, 并解密 数据 string token = cookieClient.GetCookie(AuthConfigProvider.AuthConfig.CookieName); IAlgorithmFactory algorithmFactory = new HMACSHAAlgorithmFactory(); IJsonSerializer serializer = new JsonNetSerializer(); IDateTimeProvider provider = new UtcDateTimeProvider(); IJwtValidator validator = new JwtValidator(serializer, provider); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithmFactory); TBody authUser = decoder.DecodeToObject <TBody>(token, secret, true); SignIn(authUser); return(authUser); }
public static string Encode(string secret, IDictionary <string, object> payload, JwtHashAlgorithm type = JwtHashAlgorithm.HS256) { HMACSHAAlgorithmFactory factory = new HMACSHAAlgorithmFactory(); IJwtAlgorithm algorithm = factory.Create(type); JWT.IJsonSerializer serializer = new JsonNetSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); var builder = new JwtBuilder() .WithAlgorithm(algorithm) .WithSecret(secret); if (payload != null) { foreach (var key in payload.Keys) { builder = builder.AddClaim(key, payload[key]); } } var token = builder.Build(); return(token); }
public void SignIn(TBody body) { // 获取 密钥 string secret = SecretBuilder.Build(); if (string.IsNullOrWhiteSpace(secret)) { throw new Exception("应用程序密钥(AppSecret)为空或null"); } // 生成加密token; IAlgorithmFactory algorithmFactory = new HMACSHAAlgorithmFactory(); IJwtAlgorithm algorithm = algorithmFactory.Create(AuthConfigProvider.AuthConfig.JwtAlgorithmType); IJsonSerializer serializer = new JsonNetSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder); string token = encoder.Encode(body, secret); // 写入Cookie ICookieFactory cookieFactory = new CookieFactory(); ICookieClient cookieClient = cookieFactory.Create(); cookieClient.SetCookie(AuthConfigProvider.AuthConfig.CookieName, token, AuthConfigProvider.AuthConfig.Expires); }