示例#1
0
 public ActionResult Login([Bind(Include = "UserName,Password,RememberMe")] LoginViewModel loginModel)
 {
     if (ModelState.IsValid)
     {
         var user = userService.Find(loginModel.UserName);
         if (user == null)
         {
             ModelState.AddModelError("UserName", "用户名不存在");
         }
         else if (user.Password == Security.Sha256(loginModel.Password))
         {
             user.LoginTime = System.DateTime.Now;
             user.LoginIP   = Request.UserHostAddress;
             userService.Update(user);
             //用CreateIdentity方法创建标识,然后用SignOut方法清空Cookies,然后用SignIn登录
             var identity = userService.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
             AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
             AuthenticationManager.SignIn(new AuthenticationProperties()
             {
                 IsPersistent = loginModel.RememberMe
             }, identity);
             return(RedirectToAction("Index", "Home"));
         }
         else
         {
             ModelState.AddModelError("Password", "密码错误");
         }
     }
     return(View(loginModel));
 }
示例#2
0
 public ActionResult Login(LoginViewModel loginViewModel)
 {
     if (ModelState.IsValid)
     {
         var _user = userService.Find(loginViewModel.UserName);
         if (_user == null)
         {
             ModelState.AddModelError("UserName", "用户名不存在");
         }
         else if (_user.Password == Common.Security.Sha256(loginViewModel.Password))
         {
             _user.LoginTime = System.DateTime.Now;
             _user.LoginIP   = Request.UserHostAddress;
             userService.Update(_user);
             var _identity = userService.CreateIdentity(_user, DefaultAuthenticationTypes.ApplicationCookie);
             AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
             AuthenticationManager.SignIn(new AuthenticationProperties()
             {
                 IsPersistent = loginViewModel.RememberMe
             }, _identity);
             return(RedirectToAction("Index", "Home"));
         }
         else
         {
             ModelState.AddModelError("Password", "密码错误");
         }
     }
     return(View());
 }