Example #1
0
        public string SetValidationCode(string userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentNullException("用户名为空");
            }

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

                // set a 20-lengthed validation string (code), it's only validate in 24 hours
                u.ValidationCode = (new Utilities.RandString()).Generate();
                u.validUntil     = DateTime.Now.AddDays(1);

                en.ApplyCurrentValues("aspnet_Users", u);
                en.SaveChanges();

                return(u.ValidationCode);
            }
            catch
            {
                throw;
            }
        }
Example #2
0
        public void SetEmail(string userName, string newEmail)
        {
            if (string.IsNullOrEmpty(newEmail))
            {
                throw new ArgumentException("新的Email为空");
            }

            if (GetUserNameByEmail(newEmail) != null)
            {
                throw new ArgumentException("Email已经被注册");
            }

            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);
                }
                catch
                {
                    throw new ArgumentException("用户名不存在");
                }

                u.Email             = newEmail;
                member.Email        = newEmail;
                member.LoweredEmail = newEmail.ToLower();

                en.ApplyCurrentValues("aspnet_Users", u);
                en.ApplyCurrentValues("aspnet_Membership", member);
                en.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
Example #3
0
        public void SetPhotoUrl(string userName, string photoUrl)
        {
            if (string.IsNullOrWhiteSpace(photoUrl))
            {
                throw new ArgumentNullException("头像图片地址为空");
            }

            try
            {
                var          en = new Models.Entities();
                aspnet_Users u  = en.aspnet_Users.First(m => m.UserName == userName);

                u.PhotoUrl = photoUrl;
                en.ApplyCurrentValues("aspnet_Users", u);
                en.SaveChanges();
            }
            catch
            {
                throw new ArgumentException("用户名不存在");
            }
        }
Example #4
0
        public string SetValidationCode(string userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentNullException("用户名为空");
            }

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

                // set a 20-lengthed validation string (code), it's only validate in 24 hours
                u.ValidationCode = (new Utilities.RandString()).Generate();
                u.validUntil = DateTime.Now.AddDays(1);

                en.ApplyCurrentValues("aspnet_Users", u);
                en.SaveChanges();

                return u.ValidationCode;
            }
            catch
            {
                throw;
            }
        }
Example #5
0
        public void SetPhotoUrl(string userName, string photoUrl)
        {
            if (string.IsNullOrWhiteSpace(photoUrl))
            {
                throw new ArgumentNullException("头像图片地址为空");
            }

            try
            {
                var en = new Models.Entities();
                aspnet_Users u = en.aspnet_Users.First(m => m.UserName == userName);

                u.PhotoUrl = photoUrl;
                en.ApplyCurrentValues("aspnet_Users", u);
                en.SaveChanges();
            }
            catch
            {
                throw new ArgumentException("用户名不存在");
            }
        }
Example #6
0
        public void SetEmail(string userName, string newEmail)
        {
            if (string.IsNullOrEmpty(newEmail))
            {
                throw new ArgumentException("新的Email为空");
            }

            if (GetUserNameByEmail(newEmail) != null)
            {
                throw new ArgumentException("Email已经被注册");
            }

            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);
                }
                catch
                {
                    throw new ArgumentException("用户名不存在");
                }

                u.Email = newEmail;
                member.Email = newEmail;
                member.LoweredEmail = newEmail.ToLower();

                en.ApplyCurrentValues("aspnet_Users", u);
                en.ApplyCurrentValues("aspnet_Membership", member);
                en.SaveChanges();
            }
            catch
            {
                throw;
            }
        }
Example #7
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;
            }
        }
Example #8
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;
            }
        }