Пример #1
0
        public void SignOut()
        {
            ICookieFactory cookieFactory = new CookieFactory();
            ICookieClient  cookieClient  = cookieFactory.Create();

            cookieClient.Remove(AuthConfigProvider.AuthConfig.CookieName);
        }
Пример #2
0
        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);
        }
Пример #3
0
        public void SignIn(TBody body)
        {
            //  获取 密钥
            string secret = SecretBuilder.Build();

            if (string.IsNullOrWhiteSpace(secret))
            {
                throw new Exception("应用程序密钥(AppSecret)为空或null");
            }

            ICookieFactory cookieFactory = new CookieFactory();
            ICookieClient  cookieClient  = cookieFactory.Create();

            cookieClient.SetCookie(AuthConfigProvider.AuthConfig.CookieName, body.SerializeObject(), AuthConfigProvider.AuthConfig.Expires,
                                   value =>
            {
                ICryptor cryptor = new DesCryptor(SecretBuilder.AppKey, secret);
                return(cryptor.Encrypt(value));
            });
        }
Пример #4
0
        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);
        }
Пример #5
0
        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);
            }

            string token = cookieClient.GetCookie(AuthConfigProvider.AuthConfig.CookieName, value =>
            {
                ICryptor cryptor = new DesCryptor(SecretBuilder.AppKey, secret);
                return(cryptor.Decrypt(value));
            });

            TBody authUser = token.DeserializeObject <TBody>();

            DateTime expires = authUser.exp.AsDateTime();

            if (expires < DateTime.Now)
            {
                return(null);                           // 已失效
            }
            SignIn(authUser);

            return(authUser);
        }