예제 #1
0
        public ActionResult RegisterView(RegisterationClass model)
        {
            string pass = model.Password;
            EncryPass encpass = new EncryPass();
            pass= encpass.GetHashString(pass);
            if (ModelState.IsValid)
            {
                User user = null;
                using (DataContext db = new DataContext())
                {
                    
                    user = db.Users.FirstOrDefault(u => u.Login == model.Login);
                }
                if (user == null)
                {
                    db.Users.Add(new User { Login = model.Login, Password = pass });
                    db.SaveChanges();
                    user = db.Users.Where(u => u.Login == model.Login && u.Password == pass).FirstOrDefault();
                    id = user.Id;
                    if (user != null)
                    {
                        FormsAuthentication.SetAuthCookie(model.Login, true);

                        return RedirectToAction("PersonalOffice", "Account");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Пользователь с таким логином уже существует");
                }
            }
            return View(model);
        }
예제 #2
0
        public ActionResult LoginView(LoginClass model, string returnUrl)
        {
            
            if (ModelState.IsValid)
            {
                User user = null;
                using (DataContext db = new DataContext())
                {
                    string userEmail = model.Login;
                    string Password = model.Password;
                    EncryPass encpass = new EncryPass();
                    Password = encpass.GetHashString(Password);
                    bool userValid = db.Users.Any(u => u.Login == userEmail && u.Password == Password);
                    if (userValid)
                    {
                        FormsAuthentication.SetAuthCookie(userEmail, false);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            user = db.Users.FirstOrDefault(u => u.Login == userEmail && u.Password == Password);
                            id = user.Id;
                            return RedirectToAction("PersonalOffice", "Account");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Пользователя с таким логином и паролем нет");
                    }
                }
            }

            return View(model);
        }