Пример #1
0
        public static void ConvertPlainPasswordToHashPasswordInAllUsers()
        {
            TRAVEL_WEBDataContext datacontext = new TRAVEL_WEBDataContext();
            var taikhoans = from t in datacontext.TAI_KHOANs select t;

            foreach (TAI_KHOAN tk in taikhoans)
            {
                tk.Salt = CryptographicHelper.CreateSalt();
                if (String.IsNullOrEmpty(tk.Password))
                {
                    tk.Password = Membership.GeneratePassword(10, 3);
                }
                tk.Password = CryptographicHelper.CreatePasswordHash(tk.Password, tk.Salt);
            }
            datacontext.SubmitChanges();
        }
        public override bool ValidateUser(string username, string password)
        {
            TRAVEL_WEBDataContext dataContext = new TRAVEL_WEBDataContext();
            var user = from u in dataContext.TAI_KHOANs
                       where u.Username == username
                       select u;

            if (user.Count() == 1)
            {
                TAI_KHOAN tk = user.Single();
                if (tk.Password.Equals(CryptographicHelper.CreatePasswordHash(password, tk.Salt)))
                {
                    return(true);
                }
            }
            return(false);
        }
        public override string ResetPassword(string username, string answer)
        {
            string newPassword = "";
            TRAVEL_WEBDataContext dataContext = new TRAVEL_WEBDataContext();
            var user = from u in dataContext.TAI_KHOANs
                       where u.Username == username
                       select u;

            if (user.Count() == 1)
            {
                //newPassword = CryptographicHelper.GenerateKey();
                newPassword = Membership.GeneratePassword(10, 3);
                TAI_KHOAN tk = user.Single();
                tk.Password = CryptographicHelper.CreatePasswordHash(newPassword, tk.Salt);
                dataContext.SubmitChanges();
            }
            return(newPassword);
        }
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            TRAVEL_WEBDataContext dataContext = new TRAVEL_WEBDataContext();
            var user = from u in dataContext.TAI_KHOANs
                       where u.Username == username
                       select u;

            if (user.Count() == 1)
            {
                TAI_KHOAN tk = user.Single();
                oldPassword = CryptographicHelper.CreatePasswordHash(oldPassword, tk.Salt);
                newPassword = CryptographicHelper.CreatePasswordHash(newPassword, tk.Salt);
                if (oldPassword == tk.Password && oldPassword != newPassword)
                {
                    tk.Password = newPassword;
                    dataContext.SubmitChanges();
                    return(true);
                }
            }
            return(false);
        }