예제 #1
0
        public ActionResult LogIn(LogInModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var result = UserInfoRepo.Login(model.UserName, model.Password);

            if (result.HasError)
            {
                ViewBag.Error = result.Message;
                return(View(model));
            }
            var userProfile = new UserProfile()
            {
                ID         = result.Data.ID,
                Name       = result.Data.Name,
                UserName   = result.Data.UserName,
                Email      = result.Data.Email,
                UserTypeID = result.Data.UserTypeID
            };
            var UserProfileJason = JsonConvert.SerializeObject(userProfile);

            FormsAuthentication.SetAuthCookie(UserProfileJason, false);
            return(RedirectToAction("Home", "Main"));
        }
        public ActionResult Login(LoginModel loginModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(loginModel));
            }

            MD5 md5 = MD5.Create();

            byte[]        bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(loginModel.Pass);
            byte[]        hash  = md5.ComputeHash(bytes);
            StringBuilder sb    = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            var HashLoginPass = sb.ToString();

            var result = UserInfoRepo.Login(loginModel.CEmail, HashLoginPass);

            if (result.HasError)
            {
                ViewBag.Error = result.Message;
                return(View(loginModel));
            }

            var userProfile = new UserProfile()
            {
                ID             = result.Data.UID,
                FirstName      = result.Data.FirstName,
                LastName       = result.Data.LastName,
                UserTypeID     = result.Data.UserTypeID,
                UEmail         = result.Data.UEmail,
                CEmail         = result.Data.CompanyInfo.CEmail,
                PreferredRoe   = result.Data.PreferredRoe,
                CName          = result.Data.CompanyInfo.CName,
                TradeLicenseNo = result.Data.CompanyInfo.TradeLicenseNo,
                Website        = result.Data.CompanyInfo.Website,
                PhnNumber      = result.Data.CompanyInfo.PhnNumber,
                Address        = result.Data.CompanyInfo.Address,
                City           = result.Data.CompanyInfo.City,
                Zip            = result.Data.CompanyInfo.Zip
            };

            var upJson = JsonConvert.SerializeObject(userProfile);

            FormsAuthentication.SetAuthCookie(upJson, false);

            if (userProfile.UserTypeID == (int)EnumCollection.UserTypeEnum.Admin)
            {
                return(RedirectToAction("Index", "Admin"));
            }
            else if (userProfile.UserTypeID == (int)EnumCollection.UserTypeEnum.Agent)
            {
                return(RedirectToAction("Index", "Agent"));
            }
            else
            {
                return(RedirectToAction("Logout"));
            }
        }