예제 #1
0
        public ActionResult Create(AccountModelView add)
        {
            if (ModelState.IsValid)
            {
                db = new AccountDAL();
                User user = new User();
                user.Name     = add.Name;
                user.Username = add.Username;
                user.Email    = add.Email;
                user.Addrress = add.Address;
                user.Mobile   = add.Phone;
                string seretKey = Utility.RandomString(8);
                user.UPassword = Securities.EncryptPassword(add.Password + seretKey);
                int rs = db.Insert(user);
                switch (rs)
                {
                case 1: ViewData["SuccessMsg"] = "Thêm thành công!"; break;

                case 0: ViewData["ErrorMsg"] = "Thêm không thành công!"; break;

                case 2: ViewData["ErrorMsg"] = "Thêm không thành công, Tài khoản bị trùng!"; break;

                default: ViewData["ErrorMsg"] = "Thêm không thành công, Có phát sinh lỗi!"; break;
                }
                string btnAction = Request.Form["btnSave"].ToString();
                if (btnAction == "SaveAndCreate")
                {
                    return(RedirectToAction("Create"));
                }
                return(RedirectToAction("Index"));
            }
            return(View(add));
        }
예제 #2
0
        /// <summary>
        /// Login Website
        /// </summary>
        /// <param name="username">username or email</param>
        /// <param name="password">password login</param>
        /// <param name="login">AccountModel</param>
        /// <returns></returns>
        public bool Login(string username, string password, out AccountModel login)
        {
            try
            {
                User user = new User();
                db = new THN_WebApplicationEntities();
                AccountModel model = new AccountModel();
                if (EmailHelper.IsEmail(username))
                {
                    user = db.Users.Where(u => u.Email == username).FirstOrDefault();
                }
                else
                {
                    user = db.Users.Where(u => u.Username.Contains(username)).FirstOrDefault();
                }
                if (user == null)
                {
                    login = model;
                    return(false);
                }
                else
                {
                    string seretKey = user.SeretKey;
                    string pssword  = Securities.EncryptPassword(password + seretKey);
                    if (user.UPassword.Contains(pssword))
                    {
                        user.IPAddress = Utility.GetClientIP();
                        user.LastLogin = DateTime.Now;

                        db.SaveChanges();

                        model.ID            = (int)user.ID;
                        model.Username      = user.Username;
                        model.Email         = user.Email;
                        model.Name          = user.Name;
                        model.Address       = user.Addrress;
                        model.Phone         = user.Mobile;
                        model.IPAdress      = user.IPAddress;
                        model.LastDateLogin = (DateTime)user.LastLogin;
                    }
                    login = model;
                }

                if (login != null && !string.IsNullOrEmpty(login.Username) && login.ID > 0)
                {
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            {
                WriteLog(ex);
                login = new AccountModel();
                return(false);
            }
        }