예제 #1
0
        public ActionResult Login(Models.LoginViewModel model)
        {
            Random          r      = new Random();
            RefreshToken    rt     = new RefreshToken();
            M_TokenTable    mtt    = new M_TokenTable();
            M_EmployeeLogin obj    = new M_EmployeeLogin();
            M_Online        mol    = new M_Online();
            string          ErrMsg = string.Empty;

            if (B_EmployeeLogin.Login(ref obj, model.LoginTel, model.Password, ref ErrMsg) != -1)
            {
                try
                {
                    mol.PastTime  = (long)DateTime.UtcNow.AddMinutes(30).Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
                    mol.LastLogin = DateTime.Now;
                    mol.Admin_BL  = 1;
                    mol.Token     = r.NextDouble().ToString();
                    B_Online.Insert(ref mol, ref ErrMsg);
                    rt.iss = "lzfyhgm";
                    rt.exp = (long)DateTime.UtcNow.AddDays(30).Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
                    rt.iat = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;;
                    byte[] key = AES.Md5((rt.exp * r.NextDouble()).ToString(), AES.Md5DataType.t32);
                    byte[] IV  = AES.Md5((rt.iat * r.NextDouble()).ToString(), AES.Md5DataType.t16);
                    mtt.AesIV   = Convert.ToBase64String(key);
                    mtt.AesKey  = Convert.ToBase64String(IV);
                    mtt.Exp     = rt.exp;
                    mtt.User_ID = obj.ID;
                    mtt.Sign    = RefreshToken.Sign(rt);
                    B_TokenTable.Insert(ref mtt, ref ErrMsg);
                    string refreshtoken = RefreshToken.CreateReftoken(rt, key, IV, obj.ID);
                    string user_agent   = HttpContext.Request.Headers.Get("User-Agent");
                    string token        = RefreshToken.CreateToken(obj.ID, user_agent, mol.Token);
                    UseCookie.Add("UserName", model.LoginTel, DateTime.Now.AddMinutes(30));
                    UseSession.Add("UserName", model.LoginTel);
                    UseCookie.Add("UserID", obj.ID.ToString(), DateTime.Now.AddMinutes(30));
                    UseSession.Add("UserID", obj.ID.ToString());
                    UseCookie.Add("token", token, DateTime.Now.AddMinutes(30));
                    UseSession.Add("token", token);
                    UseCookie.Add("RefreshToken", refreshtoken, DateTime.Now.AddDays(30));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    return(View());
                }
                return(RedirectToAction("Index"));
            }
            Response.Write(ErrMsg);
            return(View());
        }