Example #1
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));
            });
        }
Example #2
0
        public ICryptor Create(string algorithm)
        {
            ICryptor cryptor;

            switch (algorithm.Sanitize())
            {
            case "aes":
                cryptor = new AesCryptor();
                break;

            case "des":
                cryptor = new DesCryptor();
                break;

            case "3des":
                cryptor = new TDesCryptor();
                break;

            default:
                throw new Exception();
            }

            return(cryptor);
        }
Example #3
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);
        }