示例#1
0
        public bool DeleteUser(string userName)
        {
            try
            {
                var               en     = new Models.Entities();
                aspnet_Users      u      = null;
                aspnet_Membership member = null;
                try
                {
                    u      = en.aspnet_Users.First(m => m.UserName == userName);
                    member = en.aspnet_Membership.First(m => m.UserId == u.UserId);

                    en.DeleteObject(member);
                    en.DeleteObject(u);

                    en.SaveChanges();

                    return(true);
                }
                catch
                {
                    throw new ArgumentException("注销失败");
                }
            }
            catch
            {
                throw;
            }
        }
示例#2
0
        public void ResetPassword(string validationCode, string newPassword)
        {
            if (string.IsNullOrEmpty(validationCode))
            {
                throw new ArgumentNullException("验证码为空");
            }

            if (string.IsNullOrEmpty(newPassword))
            {
                throw new ArgumentNullException("新密码不能为空");
            }

            if (newPassword.Length < 6)
            {
                throw new ArgumentNullException("新密码不能少于6个字符");
            }

            try
            {
                var en = new Models.Entities();
                aspnet_Users u = null;
                try
                {
                    u = en.aspnet_Users.First(m => m.ValidationCode == validationCode);
                }
                catch
                {
                    throw new ArgumentException("用户名不存在");
                }

                if (((DateTime)u.validUntil).CompareTo(DateTime.Now) < 0)
                {
                    throw new ArgumentException("验证码过期");
                }

                aspnet_Membership member = en.aspnet_Membership.First(m => m.UserId == u.UserId);

                var userName = u.UserName;
                var email = member.Email;

                var cd = member.CreateDate;
                var lld = member.LastLoginDate;
                var unc = u.UserNameChanged;

                // delete old user
                en.DeleteObject(member);
                en.DeleteObject(u);

                en.SaveChanges();

                // recreate the user
                CreateUser(userName, newPassword, email);

                // copy the old user's attrs to the new one
                try
                {
                    var newU = en.aspnet_Users.First(m => m.UserName == userName);
                    var newMember = en.aspnet_Membership.First(m => m.UserId == newU.UserId);

                    newU.UserNameChanged = unc;
                    newMember.CreateDate = cd;
                    newMember.LastLoginDate = lld;

                    en.ApplyCurrentValues("aspnet_Users", newU);
                    en.ApplyCurrentValues("aspnet_Membership", newMember);

                    en.SaveChanges();
                }
                catch
                {
                    throw new ArgumentException("用户名不存在");
                }

                return;
            }
            catch
            {
                throw;
            }
        }
示例#3
0
        public bool DeleteUser(string userName)
        {
            try
            {
                var en = new Models.Entities();
                aspnet_Users u = null;
                aspnet_Membership member = null;
                try
                {
                    u = en.aspnet_Users.First(m => m.UserName == userName);
                    member = en.aspnet_Membership.First(m => m.UserId == u.UserId);

                    en.DeleteObject(member);
                    en.DeleteObject(u);

                    en.SaveChanges();

                    return true;
                }
                catch
                {
                    throw new ArgumentException("注销失败");
                }
            }
            catch
            {
                throw;
            }
        }
示例#4
0
        public void ResetPassword(string validationCode, string newPassword)
        {
            if (string.IsNullOrEmpty(validationCode))
            {
                throw new ArgumentNullException("验证码为空");
            }

            if (string.IsNullOrEmpty(newPassword))
            {
                throw new ArgumentNullException("新密码不能为空");
            }

            if (newPassword.Length < 6)
            {
                throw new ArgumentNullException("新密码不能少于6个字符");
            }

            try
            {
                var          en = new Models.Entities();
                aspnet_Users u  = null;
                try
                {
                    u = en.aspnet_Users.First(m => m.ValidationCode == validationCode);
                }
                catch
                {
                    throw new ArgumentException("用户名不存在");
                }

                if (((DateTime)u.validUntil).CompareTo(DateTime.Now) < 0)
                {
                    throw new ArgumentException("验证码过期");
                }

                aspnet_Membership member = en.aspnet_Membership.First(m => m.UserId == u.UserId);

                var userName = u.UserName;
                var email    = member.Email;

                var cd  = member.CreateDate;
                var lld = member.LastLoginDate;
                var unc = u.UserNameChanged;

                // delete old user
                en.DeleteObject(member);
                en.DeleteObject(u);

                en.SaveChanges();

                // recreate the user
                CreateUser(userName, newPassword, email);

                // copy the old user's attrs to the new one
                try
                {
                    var newU      = en.aspnet_Users.First(m => m.UserName == userName);
                    var newMember = en.aspnet_Membership.First(m => m.UserId == newU.UserId);

                    newU.UserNameChanged    = unc;
                    newMember.CreateDate    = cd;
                    newMember.LastLoginDate = lld;

                    en.ApplyCurrentValues("aspnet_Users", newU);
                    en.ApplyCurrentValues("aspnet_Membership", newMember);

                    en.SaveChanges();
                }
                catch
                {
                    throw new ArgumentException("用户名不存在");
                }

                return;
            }
            catch
            {
                throw;
            }
        }