コード例 #1
0
        public ActionResult Register(User u)
        {
            UsersOperations uo = new UsersOperations();

            bool status = new AccountsController().VerifyCaptcha(this);

            if (status == false)
            {
                ViewData["error_message"] = "Google reCaptcha validation failed";

                new LogsOperations().AddLog(
                    new Log()
                {
                    Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                    Exception  = "reCaptcha Failed",
                    Time       = DateTime.Now,
                    Message    = "reCaptcha Failed"
                }
                    );
            }

            if (status)
            {
                try
                {
                    if ((u.Password.Length >= 6) && (u.Password.Length <= 15))
                    {
                        if (Regex.IsMatch(u.Password, @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,15}$"))
                        {
                            if (ModelState.IsValid)
                            {
                                uo.Register(u);
                                ViewData["success_message"] = "User registered successfully";
                                TempData["success_message"] = "User registered successfully";
                                ModelState.Clear();
                                return(RedirectToAction("Login", "Accounts"));
                            }
                            else
                            {
                                new LogsOperations().AddLog(
                                    new Log()
                                {
                                    Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                                    Exception  = "Invalid Model State",
                                    Time       = DateTime.Now,
                                    Message    = "Invalid Model State"
                                }
                                    );
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("Password", "Passwords must contain at least one digit, one uppercase and one lowercase");

                            new LogsOperations().AddLog(
                                new Log()
                            {
                                Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                                Exception  = "Weak password strength",
                                Time       = DateTime.Now,
                                Message    = "Weak password strength"
                            }
                                );
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Password", "Passwords must be between 6 and 15 characters long");

                        new LogsOperations().AddLog(
                            new Log()
                        {
                            Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                            Exception  = "Invalid password length",
                            Time       = DateTime.Now,
                            Message    = "Invalid password length"
                        }
                            );
                    }
                }
                catch (UsernameExistsException ex)
                {
                    ModelState.AddModelError("Username", ex.Message);

                    new LogsOperations().AddLog(
                        new Log()
                    {
                        Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                        Exception  = ex.Message.ToString(),
                        Time       = DateTime.Now,
                        Message    = ex.Message.ToString()
                    }
                        );
                }
                catch (Exception ex)
                {
                    ViewData["error_message"] = "User registration failed";

                    new LogsOperations().AddLog(
                        new Log()
                    {
                        Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                        Exception  = ex.Message.ToString(),
                        Time       = DateTime.Now,
                        Message    = ex.Message.ToString()
                    }
                        );
                }
            }
            return(View());
        }
コード例 #2
0
        public ActionResult Login(string username, string password)
        {
            if (new UsersOperations().IsBlocked(username))
            {
                int time = new UsersOperations().RemainingBlocked(username);
                ViewData["error_message"] = "You are blocked for " + time + " more minutes. Try again later.";

                new LogsOperations().AddLog(
                    new Common.Log()
                {
                    Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                    Exception  = "User blocked",
                    Time       = DateTime.Now,
                    Message    = username + " tried to log in but is currently blocked"
                }
                    );
            }
            else
            {
                bool status = false;
                try
                {
                    status = new AccountsController().VerifyCaptcha(this);
                }
                catch (Exception e)
                {
                    ViewData["error_message"] = e.Message;

                    return(View());
                }

                if (status == false)
                {
                    ViewData["error_message"] = "Google reCaptcha validation failed";

                    new LogsOperations().AddLog(
                        new Common.Log()
                    {
                        Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                        Exception  = "reCaptcha Failed",
                        Time       = DateTime.Now,
                        Message    = username + ": reCaptcha Failed"
                    }
                        );
                }

                if (status)
                {
                    if (new UsersOperations().Login(username, password) == true)
                    {
                        FormsAuthentication.SetAuthCookie(username, true);
                        return(RedirectToAction("Index", "Documents"));
                    }
                    else
                    {
                        new LogsOperations().AddLog(
                            new Common.Log()
                        {
                            Controller = RouteData.Values["controller"].ToString() + "/" + RouteData.Values["action"].ToString(),
                            Exception  = "Invalid Password",
                            Time       = DateTime.Now,
                            Message    = username + ": Invalid Password"
                        }
                            );
                        ViewData["message"] = "An invalid username or password was entered. Please try again.";
                    }
                }
            }

            return(View());
        }