/// <summary>
        /// 申请找回密码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="email">电子邮箱</param>
        /// <param name="userip">用户IP</param>
        /// <param name="checkCode">验证码</param>
        /// <param name="link">找回密码链接</param>
        /// <returns>是否可以申请</returns>
        public static async Task <IMethodResult> RequestResetUserPassword(String userName, String email, String userip, String checkCode, String link)
        {
            if (!CheckCodeStatus.VerifyCheckCode(checkCode))
            {
                return(MethodResult.Failed("The verification code you input didn't match the picture, Please try again!"));
            }

            if (!RegexVerify.IsUserName(userName))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            if (!RegexVerify.IsEmail(email))
            {
                return(MethodResult.Failed("Email address is INVALID!"));
            }

            UserEntity user = UserManager.InternalGetUserByNameAndEmail(userName, email);

            if (user == null)
            {
                return(MethodResult.Failed("The username \"{0}\" doesn't exist or the email is wrong!", userName));
            }

            if (user.IsLocked)
            {
                return(MethodResult.Failed("The user is locked, please contact the administrator!"));
            }

            if (String.IsNullOrEmpty(user.Email) || "NULL".Equals(user.Email, StringComparison.OrdinalIgnoreCase))
            {
                return(MethodResult.Failed("The user has no email, please contact the administrator!"));
            }

            Random rand = new Random(DateTime.Now.Millisecond);

            UserForgetPasswordEntity ufp = new UserForgetPasswordEntity()
            {
                UserName   = userName,
                SubmitDate = DateTime.Now,
                SubmitIP   = userip,
                HashKey    = MD5Encrypt.EncryptToHexString(String.Format("{0}-{1}-{2}", userName, DateTime.Now.Ticks.ToString(), rand.Next(DateTime.Now.Millisecond)), true)
            };

            Boolean success = UserForgetPasswordRepository.Instance.InsertEntity(ufp) > 0;

            if (!success)
            {
                return(MethodResult.Failed("Failed to process your request!"));
            }

            String url         = ConfigurationManager.DomainUrl + ((link[0] == '/') ? link.Substring(1) : link);
            String mailSubject = ConfigurationManager.OnlineJudgeName + " Password Recovery";
            String mailContent = UserForgetPasswordManager.GetMailContent(userName, url + ufp.HashKey.ToLowerInvariant());

            try
            {
                await MailClient.SendMailAsync(ConfigurationManager.EmailSMTPServer, ConfigurationManager.EmailAddresser, email, mailSubject, mailContent, true, true, ConfigurationManager.EmailUsername, ConfigurationManager.EmailPassword);
            }
            catch
            {
                return(MethodResult.Failed("Failed to send a password reset link to your email address."));
            }

            return(MethodResult.SuccessAndLog("User forget password, name = {0}", userName));
        }
Beispiel #2
0
        /// <summary>
        /// 尝试注册用户
        /// </summary>
        /// <param name="entity">用户实体</param>
        /// <param name="password">密码</param>
        /// <param name="password2">重复密码</param>
        /// <param name="checkCode">验证码</param>
        /// <param name="userip">用户IP</param>
        /// <returns>执行结果</returns>
        public static IMethodResult SignUp(UserEntity entity, String password, String password2, String checkCode, String userip)
        {
            if (!CheckCodeStatus.VerifyCheckCode(checkCode))
            {
                return(MethodResult.Failed("The verification code you input didn't match the picture, Please try again!"));
            }

            if (String.IsNullOrEmpty(entity.UserName))
            {
                return(MethodResult.Failed("Username can not be NULL!"));
            }

            if (!RegexVerify.IsUserName(entity.UserName) || !SQLValidator.IsNonNullANDSafe(entity.UserName))
            {
                return(MethodResult.Failed("Username can not contain illegal characters!"));
            }

            if (!KeywordsFilterManager.IsUserNameLegal(entity.UserName))
            {
                return(MethodResult.Failed("Username can not contain illegal keywords!"));
            }

            if (entity.UserName.Length > UserRepository.USERNAME_MAXLEN)
            {
                return(MethodResult.Failed("Username is too long!"));
            }

            if (String.IsNullOrEmpty(password))
            {
                return(MethodResult.Failed("Password can not be NULL!"));
            }

            if (!String.Equals(password, password2))
            {
                return(MethodResult.Failed("Two passwords are not match!"));
            }

            if (String.IsNullOrEmpty(entity.Email))
            {
                return(MethodResult.Failed("Email address can not be NULL!"));
            }

            if (!RegexVerify.IsEmail(entity.Email))
            {
                return(MethodResult.Failed("Email address is INVALID!"));
            }

            if (entity.Email.Length > UserRepository.EMAIL_MAXLEN)
            {
                return(MethodResult.Failed("Email address is too long!"));
            }

            if (!String.IsNullOrEmpty(entity.NickName) && entity.NickName.Length > UserRepository.NICKNAME_MAXLEN)
            {
                return(MethodResult.Failed("Nick Name is too long!"));
            }

            if (!KeywordsFilterManager.IsUserNameLegal(entity.NickName))
            {
                return(MethodResult.Failed("Nick Name can not contain illegal keywords!"));
            }

            if (!String.IsNullOrEmpty(entity.School) && entity.School.Length > UserRepository.SCHOOL_MAXLEN)
            {
                return(MethodResult.Failed("School Name is too long!"));
            }

            if (UserRepository.Instance.ExistsEntity(entity.UserName))
            {
                return(MethodResult.Failed("The username \"{0}\" has already existed!", entity.UserName));
            }

            if (!UserIPStatus.CheckLastRegisterTime(userip))
            {
                return(MethodResult.Failed("You can only register one user from single ip in {0} seconds!", ConfigurationManager.RegisterInterval.ToString()));
            }

            entity.PassWord   = PassWordEncrypt.Encrypt(entity.UserName, password);
            entity.NickName   = HtmlEncoder.HtmlEncode(entity.NickName);
            entity.Permission = PermissionType.None;
            entity.CreateIP   = userip;
            entity.CreateDate = DateTime.Now;

            try
            {
                if (UserRepository.Instance.InsertEntity(entity) == 0)
                {
                    return(MethodResult.Failed("User Registration Failed!"));
                }
            }
            catch (System.Exception ex)
            {
                return(MethodResult.Failed(ex.Message));
            }

            UserCache.RemoveRanklistUserCountCache();//删除缓存

            return(MethodResult.SuccessAndLog("User sign up"));
        }