Пример #1
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 == Security.Sha256(loginViewModel.PassWord))
         {
             _user.LoginTime = System.DateTime.Now;
             _user.LoginIP   = Request.UserHostAddress;
             userService.Update(_user);
             var _identity = userService.CreateIdentity(_user, DefaultAuthenticationTypes.ApplicationCookie);
             Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
             Authentication.SignIn(new AuthenticationProperties()
             {
                 IsPersistent = loginViewModel.RememberMe
             }, _identity);
             return(RedirectToAction("index", "Home"));
         }
         else
         {
             ModelState.AddModelError("Password", "密码错误");
         }
     }
     return(View());
 }
Пример #2
0
 public ActionResult Register(RegisterViewModel register)
 {
     if (TempData["VerificationCode"] == null || TempData["VerificationCode"].ToString() != register.VerificationCode.ToUpper())
     {
         ModelState.AddModelError("VerificationCode", "验证码不正确");
         return(View(register));
     }
     if (ModelState.IsValid)
     {
         if (userService.Exist(register.UserName))
         {
             ModelState.AddModelError("UserName", "用户名已存在");
         }
         else
         {
             User _user = new User()
             {
                 UserName = register.UserName,
                 //默认用户组代码写这里
                 DisplayName = register.DisplayName,
                 Password    = Security.Sha256(register.Password),
                 //邮箱验证与邮箱唯一性问题
                 Email = register.Email,
                 //用户状态问题
                 Status           = 0,
                 RegistrationTime = System.DateTime.Now,
                 LoginTime        = System.DateTime.Now,
                 LoginIP          = Request.UserHostAddress
             };
             _user = userService.Add(_user);
             if (_user.UserID > 0)
             {
                 var _identity = userService.CreateIdentity(_user, DefaultAuthenticationTypes.ApplicationCookie);
                 AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                 AuthenticationManager.SignIn(_identity);
                 return(RedirectToAction("Index", "Home"));
             }
             else
             {
                 ModelState.AddModelError("", "注册失败!");
             }
         }
     }
     return(View(register));
 }
Пример #3
0
 public ActionResult Register([Bind(Include = "UserID,UserName,DisplayName,Password,ConfirmPassword,Email,VerificationCode")] RegisterViewModel register)
 {
     if (TempData["checkcode"] == null || TempData["checkcode"].ToString() != register.VerificationCode.ToUpper())
     {
         ModelState.AddModelError("checkcode", "验证码错误");
         return(View(register));
     }
     if (ModelState.IsValid)
     {
         if (userService.Exist(register.UserName))
         {
             ModelState.AddModelError("UserName", "用户已经存在");
         }
         else
         {
             User user = new User()
             {
                 UserName         = register.UserName,
                 DisplayName      = register.DisplayName,
                 Password         = Security.Sha256(register.Password),
                 Emial            = register.Email,
                 Status           = 0,
                 RegistrationTime = System.DateTime.Now
             };
             user = userService.Add(user);
             if (user.UserID > 0)
             {
                 //return Content("注册成功");
                 var identity = userService.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                 AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                 AuthenticationManager.SignIn(identity);
                 return(RedirectToAction("Index", "Home"));
             }
             else
             {
                 ModelState.AddModelError("", "注册失败");
             }
         }
     }
     return(View(register));
 }