コード例 #1
0
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            try
            {
                var user = DataBaseUsers.First(u => u.Username == username);
                user.IsDeleted = true;

                if (deleteAllRelatedData)
                {
                    // TODO: !!!
                }

                var rowsAffected = _dataBase.SaveChanges();
                return(rowsAffected > 0);
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "DeleteUser");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User          user = null;
                DataBaseUsers db   = new DataBaseUsers();
                user = db.GetUser(model);

                if (user == null)
                {
                    db.AddUser(model);
                    user = db.GetUser(model);

                    if (user != null)
                    {
                        FormsAuthentication.SetAuthCookie(model.Login, true);
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Пользователь с таким логином или email существует");
                }
            }

            return(View(model));
        }
コード例 #3
0
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            try
            {
                var user = DataBaseUsers.FirstOrDefault(u => u.Username == username);
                if (user == null)
                {
                    return(null);
                }

                var msUser = ConverDataBaseUserToMemberShipUser(user);
                if (userIsOnline)
                {
                    user.LastActivityDate = DateTime.Now;
                    _dataBase.SaveChanges();
                }

                return(msUser);
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "GetUser(String, Boolean)");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #4
0
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            try
            {
                int uid;
                if (!int.TryParse(providerUserKey.ToString(), out uid))
                {
                    throw new FormatException("Provider key is in invalid format.");
                }

                var user   = DataBaseUsers.First(u => u.ID == uid);
                var msUser = ConverDataBaseUserToMemberShipUser(user);

                if (userIsOnline)
                {
                    user.LastActivityDate = DateTime.Now;
                    _dataBase.SaveChanges();
                }

                return(msUser);
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "GetUser(Object, Boolean)");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #5
0
        private void UpdateFailureCount(string username, string failureType)
        {
            try
            {
                var user         = DataBaseUsers.First(u => u.Username == username);
                var windowStart  = new DateTime();
                var failureCount = 0;



                var windowEnd = windowStart.AddMinutes(PasswordAttemptWindow);

                if (failureCount == 0 || DateTime.Now > windowEnd)
                {
                    // First password failure or outside of PasswordAttemptWindow.
                    // Start a new password failure count from 1 and a new window starting now.



                    // TODO: Throw the old exception.
                    //if (cmd.ExecuteNonQuery() < 0)
                    //    throw new ProviderException("Unable to update failure count and window start.");
                }
                else
                {
                    if (failureCount++ >= MaxInvalidPasswordAttempts)
                    {
                        // Password attempts have exceeded the failure threshold. Lock out
                        // the user.



                        // TODO: Throw the old exception.
                        //if (cmd.ExecuteNonQuery() < 0)
                        //    throw new ProviderException("Unable to lock out user.");
                    }
                    else
                    {
                        // Password attempts have not exceeded the failure threshold. Update
                        // the failure counts. Leave the window the same.


                        // TODO: Throw the old exception.
                        //if (cmd.ExecuteNonQuery() < 0)
                        //    throw new ProviderException("Unable to update failure count.");
                    }
                }
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "UpdateFailureCount");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #6
0
        public ActionResult RestorePassword(string Email)
        {
            string      blogEmail = "*****@*****.**";
            MailAddress blog      = new MailAddress(blogEmail, "Blog.com");
            MailAddress user      = new MailAddress(Email);
            MailMessage message   = new MailMessage(blog, user);

            string newPassword = PasswordGeneration();

            message.Subject = "Восстановление пароля";
            message.Body    = "Ваш новый пароль : " + newPassword + "\nС уважением, Blog.com";

            DataBaseUsers dataBaseUser = new DataBaseUsers();

            dataBaseUser.UpdatePassword(Email, newPassword);

            SmtpClient smtp = new SmtpClient("smtp.yandex.by", 587)
            {
                Credentials = new NetworkCredential(blogEmail, "blog_myblog"),
                EnableSsl   = true
            };

            smtp.Send(message);

            ModelState.AddModelError("", "Новый пароль отправлен на почту");
            return(View());
        }
コード例 #7
0
        //Get Users Button Click
        private void GetUsersBtn_Click(object sender, EventArgs e)
        {
            switch (GetUsersByComboBox.SelectedIndex)
            {
            case 0:    //All Users
                DataBaseUsers.DataSource = data.GetUserBy(UserBy.All);
                break;

            case 1:    //Only Connected
                DataBaseUsers.DataSource = data.GetUserBy(UserBy.Connected);
                break;

            case 2:    //Email
                DataBaseUsers.DataSource = data.GetUserBy(UserBy.Email, UserText.Text);
                break;

            case 3:    //Nick Name
                DataBaseUsers.DataSource = data.GetUserBy(UserBy.Name, UserText.Text);
                break;

            case 4:    //Lase Connection Date
                DataBaseUsers.DataSource = data.GetUserBy(UserDate.Value);
                break;
            }
            DataBaseUsers.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
        }
コード例 #8
0
        private void DeleteAdmin(User user)
        {
            ExitAdmin(user);

            DataBaseUsers dataBaseUser = new DataBaseUsers();

            dataBaseUser.DeleteUser(user.User_Id);
        }
コード例 #9
0
        public ActionResult Admin()
        {
            string        userName     = HttpContext.User.Identity.Name;
            DataBaseUsers dataBaseUser = new DataBaseUsers();
            var           user         = dataBaseUser.GetUser(userName);

            return(View(user));
        }
コード例 #10
0
        public override string GetPassword(string username, string answer)
        {
            if (!EnablePasswordRetrieval)
            {
                throw new ProviderException("Password Retrieval Not Enabled.");
            }

            if (PasswordFormat == MembershipPasswordFormat.Hashed)
            {
                throw new ProviderException("Cannot retrieve Hashed passwords.");
            }

            var user = DataBaseUsers.FirstOrDefault(u => u.Username == username);

            string password;
            string passwordAnswer;

            try
            {
                if (user != null)
                {
                    password       = user.Password;
                    passwordAnswer = user.PasswordAnswer;
                }
                else
                {
                    throw new MembershipPasswordException("The supplied user name is not found.");
                }
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "GetPassword");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }


            if (RequiresQuestionAndAnswer && !CheckPassword(answer, passwordAnswer))
            {
                UpdateFailureCount(username, "passwordAnswer");

                throw new MembershipPasswordException("Incorrect password answer.");
            }


            if (PasswordFormat == MembershipPasswordFormat.Encrypted)
            {
                password = UnEncodePassword(password);
            }

            return(password);
        }
コード例 #11
0
        public static void FillTables()
        {
            DataBaseCategories category = new DataBaseCategories();

            category.Insert();

            DataBaseUsers dataBaseUsers = new DataBaseUsers();

            dataBaseUsers.Insert();
        }
コード例 #12
0
        public override bool ValidateUser(string username, string password)
        {
            try
            {
                var isValid = false;
                var user    = DataBaseUsers.FirstOrDefault(u => u.Username == username && !u.IsLockedOut);

                bool   isApproved;
                string dbPassword;

                if (user != null)
                {
                    dbPassword = user.Password;
                    isApproved = user.IsApproved;
                }
                else
                {
                    return(false);
                }

                if (CheckPassword(password, dbPassword))
                {
                    if (isApproved)
                    {
                        isValid            = true;
                        user.LastLoginDate = DateTime.Now;
                        _dataBase.SaveChanges();
                    }
                }
                else
                {
                    UpdateFailureCount(username, "password");
                }

                return(isValid);
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "ValidateUser");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #13
0
        private User SaveAdmin(User user)
        {
            ExitAdmin(user);

            DataBaseUsers dataBaseUser = new DataBaseUsers();

            dataBaseUser.UpdateUser(user);

            var newUser = dataBaseUser.GetUser(user.User_Id);

            FormsAuthentication.SetAuthCookie(newUser.Login, true);

            ModelState.AddModelError("", "Пользователь изменен");
            Response.Redirect(Request.Path);
            return(newUser);
        }
コード例 #14
0
        public override bool ChangePassword(string username, string oldPwd, string newPwd)
        {
            if (!ValidateUser(username, oldPwd))
            {
                return(false);
            }

            var args = new ValidatePasswordEventArgs(username, newPwd, true);

            OnValidatingPassword(args);

            if (args.Cancel)
            {
                if (args.FailureInformation != null)
                {
                    throw args.FailureInformation;
                }

                throw new MembershipPasswordException("Change password canceled due to new password validation failure.");
            }


            int rowsAffected;

            try
            {
                var user = DataBaseUsers.First(u => u.Username == username);
                user.Password = EncodePassword(newPwd);
                user.LastPasswordChangedDate = DateTime.Now;

                rowsAffected = _dataBase.SaveChanges();
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "ChangePassword");
                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }

            return(rowsAffected > 0);
        }
コード例 #15
0
        public override string GetUserNameByEmail(string email)
        {
            try
            {
                var user = DataBaseUsers.FirstOrDefault(u => u.Email == email);

                return(user == null ? null : user.Username);
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "GetUserNameByEmail");
                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #16
0
        public override bool UnlockUser(string username)
        {
            try
            {
                var user         = DataBaseUsers.First(u => u.Username == username);
                var rowsAffected = _dataBase.SaveChanges();
                return(rowsAffected > 0);
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "UnlockUser");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #17
0
        public override int GetNumberOfUsersOnline()
        {
            try
            {
                var onlineSpan  = new TimeSpan(0, Membership.UserIsOnlineTimeWindow, 0);
                var compareTime = DateTime.Now.Subtract(onlineSpan);
                return(DataBaseUsers.Count(u => u.LastActivityDate > compareTime));
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "GetNumberOfUsersOnline");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #18
0
        public override void UpdateUser(MembershipUser user)
        {
            try
            {
                var dbUser = DataBaseUsers.First(u => u.Username == user.UserName);
                dbUser.Email      = user.Email;
                dbUser.IsApproved = user.IsApproved;
                _dataBase.SaveChanges();
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "UpdateUser");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }
コード例 #19
0
        public ActionResult Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                User          user = null;
                DataBaseUsers db   = new DataBaseUsers();
                user = db.GetUser(model);

                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(model.Login, true);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Неверный логин или пароль");
                }
            }

            return(View(model));
        }
コード例 #20
0
        public static void CreateTables()
        {
            DataBaseUsers user = new DataBaseUsers();

            user.CreateTables();

            DataBaseCategories category = new DataBaseCategories();

            category.CreateTable();

            DataBaseTegs teg = new DataBaseTegs();

            teg.CreateTable();

            DataBaseArticles article = new DataBaseArticles();

            article.CreateTable();

            DataBaseArticles_Tegs articles_Tegs = new DataBaseArticles_Tegs();

            articles_Tegs.CreateTable();
        }
コード例 #21
0
        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPwdQuestion,
                                                             string newPwdAnswer)
        {
            if (!ValidateUser(username, password))
            {
                return(false);
            }

            int rowsAffected;

            try
            {
                var user = DataBaseUsers.FirstOrDefault(u => u.Username == username);
                if (user != default(User))
                {
                    user.PasswordQuestion = newPwdQuestion;
                    user.PasswordAnswer   = newPwdAnswer;
                    rowsAffected          = _dataBase.SaveChanges();
                }
                else
                {
                    rowsAffected = 0;
                }
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "ChangePasswordQuestionAndAnswer");

                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }

            return(rowsAffected > 0);
        }
コード例 #22
0
        public override string ResetPassword(string username, string answer)
        {
            if (!EnablePasswordReset)
            {
                throw new NotSupportedException("Password reset is not enabled.");
            }

            if (answer == null && RequiresQuestionAndAnswer)
            {
                UpdateFailureCount(username, "passwordAnswer");

                throw new ProviderException("Password answer required for password reset.");
            }

            var newPassword = Membership.GeneratePassword(NEW_PASSWORD_LENGTH, MinRequiredNonAlphanumericCharacters);

            var args = new ValidatePasswordEventArgs(username, newPassword, true);

            OnValidatingPassword(args);

            if (args.Cancel)
            {
                if (args.FailureInformation != null)
                {
                    throw args.FailureInformation;
                }

                throw new MembershipPasswordException("Reset password canceled due to password validation failure.");
            }

            try
            {
                var user = DataBaseUsers.FirstOrDefault(u => u.Username == username);

                string passwordAnswer;
                if (user != null)
                {
                    if (user.IsLockedOut)
                    {
                        throw new MembershipPasswordException("The supplied user is locked out.");
                    }

                    passwordAnswer = user.PasswordAnswer;
                }
                else
                {
                    throw new MembershipPasswordException("The supplied user name is not found.");
                }

                if (RequiresQuestionAndAnswer && !CheckPassword(answer, passwordAnswer))
                {
                    UpdateFailureCount(username, "passwordAnswer");

                    throw new MembershipPasswordException("Incorrect password answer.");
                }

                user.Password = EncodePassword(newPassword);
                user.LastPasswordChangedDate = DateTime.Now;

                var rowsAffected = _dataBase.SaveChanges();

                if (rowsAffected > 0)
                {
                    return(newPassword);
                }

                throw new MembershipPasswordException("User not found, or user is locked out. Password not Reset.");
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "ResetPassword");
                    throw new ProviderException(EXCEPTION_MESSAGE);
                }

                throw;
            }
        }