Пример #1
0
 private void Login(object obj)
 {
     try
     {
         using (TastyAndHealthyContext context = new TastyAndHealthyContext())
         {
             User user = context.Users.Where(x => (x.Email == UserLogin || x.Login == UserLogin)).FirstOrDefault();
             if (user != null)
             {
                 if (EncryptionUtility.DecryptDate(user.Password) == Password)
                 {
                     MainWindow mainWindow = new MainWindow(user);
                     mainWindow.Show();
                     foreach (Window el in Application.Current.Windows)
                     {
                         if (el.ToString().Contains("Login"))
                         {
                             el.Close();
                             break;
                         }
                     }
                 }
                 else
                 {
                     MessageBox.Show("Invalid email or password!");
                 }
             }
             else
             {
                 MessageBox.Show("Invalid email or password!");
             }
         }
     }
     catch (Exception) { MessageBox.Show("Error connection to database!"); }
 }
Пример #2
0
 public void LoginServerValidation(object source, ServerValidateEventArgs args)
 {
     try
     {
         using (GuestBookContext context = new GuestBookContext())
         {
             if (context.Users.Where(x => x.Login == Login.Value).Any() ||
                 context.Users.Where(x => x.Email == Login.Value).Any())
             {
                 if (context.Users.Where(x => x.Login == Login.Value).Any())
                 {
                     user = context.Users.Where(x => x.Login == Login.Value).FirstOrDefault();
                     try
                     {
                         // шифруем пароль
                         string pass = EncryptionUtility.DecryptDate(user.Password);
                         if (pass != Password.Value)
                         {
                             args.IsValid = false;
                         }
                     }
                     catch (Exception)
                     {
                         Response.Write("<h2>Возникла ошибка при шифровании!</h2>");
                         args.IsValid = false;
                     }
                 }
                 else
                 {
                     user = context.Users.Where(x => x.Email == Login.Value).FirstOrDefault();
                     try
                     {
                         // шифруем пароль
                         string pass = EncryptionUtility.DecryptDate(user.Password);
                         if (pass != Password.Value)
                         {
                             args.IsValid = false;
                         }
                     }
                     catch (Exception)
                     {
                         Response.Write("<h2>Возникла ошибка при шифровании!</h2>");
                         args.IsValid = false;
                     }
                 }
             }
             else
             {
                 args.IsValid = false;
             }
         }
     }
     catch (Exception)
     {
         args.IsValid = false;
     }
 }
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            if (!User.Identity.IsAuthenticated)
            {
                if (String.IsNullOrEmpty(model.Login) || String.IsNullOrEmpty(model.Password))
                {
                    ModelState.AddModelError("", "All fields must be filled.");
                }
                if (ModelState.IsValid)
                {
                    User user = await repository.GetAsync <User>(x => x.Login == model.Login || x.Email == model.Login);

                    if (user != null && EncryptionUtility.DecryptDate(user.Password) == model.Password)
                    {
                        if (user.IsRegistered)
                        {
                            Session["UserRole"] = user.Role.Name;
                            FormsAuthentication.SetAuthCookie(model.Login, false);
                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            ModelState.AddModelError("", "Sorry, your registration has not been verified by the administrator yet.");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid login or password.");
                    }
                }
                return(View(model));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }