Exemplo n.º 1
0
 public void ToSymmetricEncryptTest()
 {
     foreach (var secret in secrets)
     {
         SymmetricEncrypt symmetricEncrypt = new SymmetricEncrypt(secret);
         foreach (var text in texts)
         {
             string result = symmetricEncrypt.Encrypt(text);
             Assert.AreEqual(text.ToSymmetricEncrypt(secret), result);
             Assert.AreEqual(result.ToSymmetricDecrypt(secret), symmetricEncrypt.Decrypt(result));
         }
     }
 }
Exemplo n.º 2
0
        public void SymmetricEncryptTest()
        {
            foreach (var secret in secrets)
            {
                SymmetricEncrypt symmetricEncrypt = new SymmetricEncrypt(secret);
                foreach (var text in texts)
                {
                    string result = symmetricEncrypt.Encrypt(text);
                    Assert.AreEqual(text, symmetricEncrypt.Decrypt(result));

                    TestContext.WriteLine($"[{text}]");
                    TestContext.WriteLine($"Symm: {result}");
                    TestContext.WriteLine("-------------------------------------");
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Cookie登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userId"></param>
        /// <param name="createPersistentCookie"></param>
        /// <param name="validityDays"></param>
        public void SignIn(string cookieName, string userName, int userId, bool createPersistentCookie = false, int validityDays = 10)
        {
            if (String.IsNullOrEmpty(userName))
            {
                throw new Exception("用户名为空");
            }
            var utcIssueDate  = DateTime.UtcNow;
            var utcExpireDate = utcIssueDate.AddDays((validityDays <= 0) ? 1 : validityDays);

            long issueTimeStamp  = DateTimeUtil.GetTimeStamp(utcIssueDate);
            long expireTimeStamp = DateTimeUtil.GetTimeStamp(utcExpireDate);

            StringBuilder cookieValue = new StringBuilder();

            cookieValue.Append("UserID=" + userId + "&");
            cookieValue.Append("UserName="******"&");
            cookieValue.Append("IssueDate=" + issueTimeStamp + "&");
            cookieValue.Append("ExpireDate=" + expireTimeStamp + "&");

            cookieValue.Append("sign=" + MD5Encrypt.Md5Hex(userId.ToString() + userName.ToString() + issueTimeStamp.ToString() + expireTimeStamp.ToString() + encryptKey));

            SymmetricEncrypt symenc    = new SymmetricEncrypt();
            string           cookiestr = symenc.Encrypto(cookieValue.ToString());

            string domain = "";

            try
            {
                domain = GetDomain(HttpContext.Current.Request.Url);
            }
            catch
            {
                domain = "a.cn";
            }

            HttpCookie authCookie = new HttpCookie(cookieName, cookiestr);

            authCookie.Path = "/";
            if (domain != "localhost")
            {
                authCookie.Domain = domain;
            }
            authCookie.Expires = utcExpireDate;
            HttpContext.Current.Response.Cookies.Add(authCookie);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 判断用户是否登陆
        /// </summary>
        /// <returns></returns>
        public bool IsLogin(string cookieName)
        {
            var cookie = HttpContext.Current.Request.Cookies[cookieName];

            if (cookie == null || String.IsNullOrWhiteSpace(cookie.Value))
            {
                return(false);
            }


            #region 开始自已写cookie
            SymmetricEncrypt symenc = new SymmetricEncrypt();
            var    cookievalue      = symenc.Decrypto(cookie.Value.Trim());
            int    uid;
            int    validay;
            string uname;
            return(ValidityCookie(cookievalue, out uid, out validay, out uname));

            #endregion
        }
Exemplo n.º 5
0
        /// <summary>
        /// 获得Cookie中保存的用户ID
        /// </summary>
        /// <returns></returns>
        public int GetUserID(string cookieName)
        {
            int    userId       = 0;
            int    validityDays = 0;
            string uname        = "";
            var    cookie       = HttpContext.Current.Request.Cookies[cookieName];

            if (cookie == null || String.IsNullOrWhiteSpace(cookie.Value))
            {
                return(0);
            }


            #region 开始自已写cookie
            SymmetricEncrypt symenc = new SymmetricEncrypt();
            var cookievalue         = symenc.Decrypto(cookie.Value.Trim());


            ValidityCookie(cookievalue, out userId, out validityDays, out uname);
            return(userId);

            #endregion
        }