Beispiel #1
0
 public async Task <ActionResult> SignIn(UserLoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         var pwd = UtilEncrypt.GetMd5Hash(model.Password);
         var acc = _userService.GetAll().FirstOrDefault(a => a.Username.Equals(model.UserName) && a.Password.Equals(pwd));
         if (acc != null)
         {
             StoreCookie(acc);
             if (string.IsNullOrWhiteSpace(returnUrl))
             {
                 return(RedirectToAction("Index", "Home"));
             }
             return(Redirect(returnUrl));
         }
         else
         {
             TempData[Constants.NotifyMessage] = new NotifyModel()
             {
                 Result  = false,
                 Message = string.Format(Resource.DataIsNotFound, model.UserName)
             };
             return(RedirectToAction("Login", "Account"));
         }
     }
     TempData[Constants.NotifyMessage] = new NotifyModel()
     {
         Result = false, Message = string.Format(Resource.InvalidData, Resource.User)
     };
     return(RedirectToAction("Login", "Account"));
 }
Beispiel #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (!model.Password.Equals(model.ConfirmPassword))
                    {
                        ModelState["Password"].Errors.Add("Password is not match");
                        return(RedirectToAction("Login", "Account"));
                    }
                    _userService.Insert(model);
                    var pwd = UtilEncrypt.GetMd5Hash(model.Password);

                    var acc = _userService.GetAll().FirstOrDefault(a => a.Username.Equals(model.UserName) && a.Password.Equals(pwd));
                    if (acc != null)
                    {
                        StoreCookie(acc);
                    }
                    return(RedirectToAction("Index", "Home"));
                }
                catch (Exception ex)
                {
                    var notify = new NotifyModel()
                    {
                        Result  = false,
                        Message = Resource.InternalException
                    };
                    if (ex.InnerException == null)
                    {
                        notify.Message = ex.Message;
                    }
                    TempData[Constants.NotifyMessage] = notify;
                    return(RedirectToAction("Login", "Account"));
                }
            }
            TempData[Constants.NotifyMessage] = new NotifyModel()
            {
                Result  = true,
                Message = string.Format(Resource.InvalidData, Resource.User)
            };
            return(RedirectToAction("Login", "Account"));
            // If we got this far, something failed, redisplay form
        }
Beispiel #3
0
 public async Task <ActionResult> Login(UserLoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         var pwd = UtilEncrypt.GetMd5Hash(model.Password);
         var acc = UnitOfWork.Repository <Account>().GetAll(a => a.Username.Equals(model.UserName) && a.Password.Equals(pwd)).FirstOrDefault();
         if (acc != null)
         {
             var ma = new MyAccount();
             ma.Username = acc.Username;
             ma.Fullname = acc.FullName;
             ma.Roles    = acc.Role.Name;
             var fat = new FormsAuthenticationTicket(1, "octopusAuth", DateTime.Now, DateTime.Now.AddMinutes(15), false, JsonConvert.SerializeObject(ma));
             var ck  = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat))
             {
                 Expires = DateTime.Now.AddMinutes(15)
             };
             Response.Cookies.Add(ck);
             if (string.IsNullOrWhiteSpace(returnUrl))
             {
                 return(RedirectToAction("Index", "Home", new RouteValueDictionary(Constants.AreaAdmin)));
             }
             return(Redirect(returnUrl));
         }
         else
         {
             TempData[Constants.NotifyMessage] = new NotifyModel()
             {
                 Result  = false,
                 Message = string.Format(Resource.DataIsNotFound, model.UserName)
             };
             return(RedirectToAction("Login", "Home", new RouteValueDictionary(Constants.AreaAdmin)));
         }
     }
     TempData[Constants.NotifyMessage] = new NotifyModel()
     {
         Result = false, Message = string.Format(Resource.InvalidData, Resource.User)
     };
     return(RedirectToAction("Login", "Home", new RouteValueDictionary(Constants.AreaAdmin)));
 }
 public Account Login(string username, string password)
 {
     password = UtilEncrypt.GetMd5Hash(password);
     return(Repository.GetAll(a => a.Username.Equals(username) && a.Password.Equals(password)).FirstOrDefault());
 }