Esempio n. 1
0
        public ActionResult ViewAuthFrom(AuthUserViewModel user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int checkLo = 0;
                    int chekPas = 0;
                    string md5Result = string.Empty;
                    int fff = 0;
                    var selectList = unitOfWork.UserRepository.Get().Select(x => x.Login).ToList();
                    foreach (var r in selectList)
                    {
                        if (r.Trim() != user.Login.Trim())
                        {
                            fff++; continue;
                        }
                        checkLo = 1;
                        break;
                    }

                    var hash = unitOfWork.UserRepository.Get().Select(x => x.Hash).ToList();
                    var salt = unitOfWork.UserRepository.Get().Select(x => x.Salt).ToList();

                    foreach (var h in hash)
                    {
                        foreach (var s in salt)
                        {
                            md5Result = GetHash(user.Password + s);

                            if (md5Result.Trim() != h.Trim()) continue;
                            chekPas = 1;
                            break;
                        }
                    }

                    if (checkLo == 1 && chekPas == 1)
                    {
                        FormsAuthentication.SetAuthCookie(user.Login.Trim(), false);
                        UserName = user.Login.Trim();
                        return RedirectToAction("List", "Note");
                    }
                    if (checkLo == 0 && chekPas == 1)
                    {
                        ModelState.AddModelError("", "Вы ввели неверный логин.");
                        //return Redirect("Index");
                    }
                    else if (checkLo == 1 && chekPas == 0)
                    {
                        ModelState.AddModelError("", "Вы ввели неверный пароль.");
                    }
                    else if (checkLo == 0 && chekPas == 0)
                    {
                        ModelState.AddModelError("", "Вы ввели неверный логин и пароль.");

                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "При сохранении возникла ошибка.");
            }
            return View();
        }
Esempio n. 2
0
        public ActionResult Index(RegistrationUserViewModel registrationUserViewModel)
        {
            //try
            //{
            if (ModelState.IsValid)
            {
                var lg = unitOfWork.UserRepository.Get().Select(x => x.Login).ToList();
                var em = unitOfWork.UserRepository.Get().Select(x => x.Email).ToList();
                int countMatchesLogin = 0;
                int countMatchesEmail = 0;
                foreach (var l in lg)
                {
                    if (l.Trim() != registrationUserViewModel.Login.Trim()) continue;
                    countMatchesLogin = 1;
                    break;
                }
                foreach (var l in em)
                {
                    if (l.Trim() != registrationUserViewModel.Email.Trim()) continue;
                    countMatchesEmail = 1;
                    break;
                }
                if (countMatchesEmail == 1)
                {
                    ModelState.AddModelError("", "Такой емаил уже существует.");
                }
                if (countMatchesLogin == 1)
                {
                    ModelState.AddModelError("", "Такой логин уже существует.");
                }
                var user = new User();
                user.Login = registrationUserViewModel.Login.Trim();
                string newSalt = GenerateSalt();
                user.Hash = GetHashForRegistration(registrationUserViewModel.Password + newSalt);
                user.Email = registrationUserViewModel.Email;
                user.Salt = newSalt;
                if (ModelState.IsValid)
                {
                    unitOfWork.UserRepository.Insert(user);
                    unitOfWork.Save();

                    LoginController loginController = new LoginController(unitOfWork);
                    AuthUserViewModel authUserViewModel = new AuthUserViewModel();
                    authUserViewModel.Login = registrationUserViewModel.Login.Trim();
                    authUserViewModel.Password = registrationUserViewModel.Password;
                    return loginController.ViewAuthFrom(authUserViewModel);
                }
            }

            return View();
        }