Exemplo n.º 1
0
        public void WhenDecryptAWordItMustBeEqualsBefore()
        {
            var word = "Murilo";

            var encryptedWord = _desCrypt.Encrypt(word);
            var decryptedWord = _desCrypt.Decrypt(encryptedWord);

            Assert.AreEqual(word, decryptedWord);
        }
        public ActionResult Login(LoginModel login)
        {
            var users = _repository.GetAll();

            if (!users.Any(u => u.Username == login.Username && _desEncryptor.Decrypt(u.Password) == login.Password))
            {
                this.ModelState.AddModelError("Username", "Usuário e senha não conferem!");
                return(View(login));
            }
            FormsAuthentication.SetAuthCookie(login.Username, true);
            return(Redirect("/Home"));
        }
Exemplo n.º 3
0
        public void EncryptionRoundTrip()
        {
            string data = "Hello World!";
              string plainTextData = string.Copy(data);
              string encryptedData;

              var encryptor = new DESEncryptor()
                        {
                          EncryptionKey = "12345678"
                        };

              encryptedData = encryptor.Encrypt(plainTextData);
              plainTextData = encryptor.Decrypt(encryptedData);

              Assert.AreEqual(data, plainTextData);
              Assert.AreNotEqual(data, encryptedData);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 检验是否登录,如果已登录,把用户信息放到session里。
        /// </summary>
        public bool CheckLogin()
        {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            bool isLogin = true;

            if (context.Request.Cookies[idCookieKey] == null || context.Request.Cookies[idCookieKey].Value == "")
            {
                isLogin = false;
            }
            else
            {
                int            uid     = Int32.Parse(DESEncryptor.Decrypt(context.Request.Cookies[idCookieKey].Value));
                Model.Customer cusInfo = new Monitor().GetCustomerInfoByUserId(uid);
                context.Session[sessionKey] = cusInfo;
            }
            return(isLogin);
        }
Exemplo n.º 5
0
        private bool IsLogin(ClaimsIdentity identity)
        {
            var strCookieKey = _navigationService.GetLoginKey();

            // 如果Cookie存在,获取cookie值。
            //如果Session存在且与cookie信息一致,则返回true,否则并将用户信息保存到Session中,返回true。
            if (_context.Request.Cookies[_cookieName] != null &&
                !string.IsNullOrEmpty(_context.Request.Cookies[_cookieName]))
            {
                try
                {
                    string cookieValue = DESEncryptor.Decrypt(_context.Request.Cookies[_cookieName], strCookieKey);
                    if (string.IsNullOrEmpty(cookieValue))
                    {
                        Logger.LogError("IsLogin无法获取cookie");
                        return(false);
                    }
                    string[] strCookieArr = cookieValue.Split('|');
                    if (strCookieArr.Length == 0)
                    {
                        Logger.LogError("IsLogin--》cookie格式不对:" + cookieValue);
                        return(false);
                    }
                    string useridstr = DESEncryptor.Decrypt(strCookieArr[0], strCookieKey);

                    int userid;
                    if (int.TryParse(useridstr, out userid))
                    {
                        string token = _navigationService.GetUserLoginToken(userid);
                        if (string.Equals(token, DESEncryptor.Decrypt(strCookieArr[7], strCookieKey)))
                        {
                            userid = Convert.ToInt32(DESEncryptor.Decrypt(strCookieArr[0], strCookieKey));
                            var username = DESEncryptor.Decrypt(WebUtility.UrlDecode(strCookieArr[1]), strCookieKey);
                            var truename = DESEncryptor.Decrypt(WebUtility.UrlDecode(strCookieArr[2]), strCookieKey);
                            var sysID    = "";
                            var gid      = DESEncryptor.Decrypt(strCookieArr[4], strCookieKey);
                            var departID = DESEncryptor.Decrypt(strCookieArr[5], strCookieKey);
                            var pid      = DESEncryptor.Decrypt(strCookieArr[6], strCookieKey);
                            var token1   = DESEncryptor.Decrypt(strCookieArr[7], strCookieKey);
                            var adname   = DESEncryptor.Decrypt(strCookieArr[8], strCookieKey);
                            var usercode = DESEncryptor.Decrypt(strCookieArr[9], strCookieKey);

                            identity.AddClaim(new Claim("userId", userid.ToString()));
                            identity.AddClaim(new Claim(ClaimTypes.Name, truename));
                            identity.AddClaim(new Claim("truename", truename));
                            identity.AddClaim(new Claim("sysID", sysID));
                            identity.AddClaim(new Claim("gid", gid));
                            identity.AddClaim(new Claim("departID", departID));
                            identity.AddClaim(new Claim("pid", pid));
                            identity.AddClaim(new Claim("token", token1));
                            identity.AddClaim(new Claim("adname", adname));
                            identity.AddClaim(new Claim("usercode", usercode));

                            return(true);
                        }
                        return(false);
                    }
                    return(false);
                }
                catch (Exception e)
                {
                    Logger.LogError("IsLogin异常", e);
                    throw;
                }
            }

            return(false);
        }
Exemplo n.º 6
0
    public string DecryptAttribute(XmlElement element, string name)
    {
        string data = element.GetAttribute(name);

        return(DESEncryptor.Decrypt(data));
    }