protected void btnSubmit_Click(object sender, EventArgs e) { try { CAPTCHA.Validate(); if (CAPTCHA.UserValidated) { MembershipUser user = Membership.GetUser(txtUserName.Text); if (user == null) { ErrorLabel.Text = "Unknown userid"; } else { string password = user.ResetPassword(); SendMailDL.SendMail(user.Email, user.Email, "Reset password from Art Gallery Site", string.Format("Your new password is {0}", password), false); ErrorLabel.Text = "Your new password has been emailed to you."; } } else { ErrorLabel.Text = "The code you entered did not match up with the image provided; please try again with this new image."; } } catch (Exception ex) { ErrorLabel.Text = "Process Error"; ErrorLogDL.Insert(ex); } up1.Update(); }
protected void Page_Load(object sender, EventArgs e) { CAPTCHA captha = new CAPTCHA(); Session["CAPTCHA"] = captha.CreatRandomString(4); Response.ClearContent(); Response.ContentType = "image/Jpeg"; Response.BinaryWrite(captha.CreatCAPTCHAImage(Session["CAPTCHA"].ToString()).ToArray()); }
protected void Page_Load(object sender, EventArgs e) { MemoryStream ms = new MemoryStream(); var img = CAPTCHA.Create("Test", 100, 50); img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Response.ClearContent(); Response.ContentType = "image/PNG"; Response.BinaryWrite(ms.GetBuffer()); }
/// <summary> /// 设置验证码 /// </summary> /// <returns></returns> public void SendCaptchas(Captcha model, ref Meta meta) { string phone = model.Phone.ToString(); if (string.IsNullOrWhiteSpace(phone)) { meta.ErrorCode = ErrorCode.SystemError.GetHashCode().ToString(); meta.ErrorMsg = "账户不能为空,请输入账号"; return; } Regex rx = new Regex("^1[34578][0-9]{9}$"); if (!rx.IsMatch(phone)) { meta.ErrorCode = ErrorCode.SystemError.GetHashCode().ToString(); meta.ErrorMsg = "请输入正确的手机号码"; return; } //用户是否注册 Users clientinfo = UserService.First(model.Phone); //登录、找回密码验证码请求需验证用户是否存在 if (((int)CaptchaType.ResetPwd).Equals((int)model.Type) || ((int)CaptchaType.Login).Equals((int)model.Type)) { if (clientinfo == null) { meta.ErrorCode = ErrorCode.NoReg.GetHashCode().ToString(); meta.ErrorMsg = "您的账号还未注册,请先注册"; return; } } //注册验证码请求需验证用户是否存在 if (((int)CaptchaType.Register).Equals((int)model.Type)) { if (clientinfo != null) { meta.ErrorCode = ErrorCode.ExistReg.GetHashCode().ToString(); meta.ErrorMsg = EnumHelper.GetDescriptionFromEnumValue(ErrorCode.ExistReg); return; } } string randomstr = WebCommon.GetRandNumber(4); if (string.Equals(phone, "13750890964")) //手机号17767076165,发送验证码8888 { randomstr = "8888"; } SMS mySms = new SMS(); mySms.SendSMS(model.Phone.ToString(), string.Format(MessageHelper.GetMessageContent("captcha", "content"), randomstr)); CAPTCHA captcha = new CAPTCHA(); if (((int)CaptchaType.Register).Equals((int)model.Type)) { captcha.Key = CaptchaType.Register + phone; } else if (((int)CaptchaType.ResetPwd).Equals((int)model.Type)) { captcha.Key = CaptchaType.ResetPwd + phone; } else if (((int)CaptchaType.Login).Equals((int)model.Type)) { captcha.Key = CaptchaType.Login + phone; } captcha.DateTime = DateTimeOffset.Now.AddMinutes(5); captcha.Value = randomstr; model.DateTime = DateTime.Now.AddMinutes(5); model.Value = randomstr; model.ID = captcha.Key; CaptchaService.Add(model, ref meta); meta.ErrorCode = ErrorCode.Success.GetHashCode().ToString(); meta.ErrorMsg = "验证码已发送至手机" + phone; }