public string VerifyCode() { try { var kvp = ValidateCoder.Mock(); _verifyCodeService.SetCode(kvp.Key, out string id); return(kvp.Value); //ValidateCoder coder = new ValidateCoder() //{ // RandomColor = true, // RandomItalic = true, // RandomLineCount = 7, // RandomPointPercent = 10, // RandomPosition = true //}; //Bitmap bitmap = coder.CreateImage(4, out string code); //_verifyCodeService.SetCode(code, out string id); //return _verifyCodeService.GetImageString(bitmap, id); } catch (Exception ex) { return(ex.Message + ex.StackTrace + ex.InnerException?.Message); } }
public void GetCodeTest() { ValidateCoder coder = new ValidateCoder(); Assert.Throws<ArgumentOutOfRangeException>(() => coder.GetCode(0)); string code = coder.GetCode(4); Assert.Equal(code.Length, 4); code = coder.GetCode(4, ValidateCodeType.Number); Assert.True(code.IsNumeric()); }
/// <summary> /// 发送验证码 /// </summary> /// <param name="input">验证码Input</param> /// <returns></returns> private async Task SendValidateCode(ValidateCodeInput input, Action <string> sendAction) { string validateCode = new ValidateCoder().GetCode(6, ValidateCodeType.Number); var codeEntity = input.MapTo <ValidateCode>(); codeEntity.Code = validateCode; await ValidateCodeRepo.InsertAsync(codeEntity); sendAction(validateCode); }
public void ValidateCoderTest() { ValidateCoder coder = new ValidateCoder(); Assert.True(coder.FontNames.Count > 0); Assert.True(coder.FontNamesForHanzi.Count > 0); Assert.True(coder.FontSize > 9); Assert.False(coder.HasBorder); Assert.False(coder.RandomPosition); Assert.False(coder.RandomItalic); }
public void GetCodeTest() { ValidateCoder coder = new ValidateCoder(); Assert.Throws <ArgumentOutOfRangeException>(() => coder.GetCode(0)); string code = coder.GetCode(4); Assert.Equal(code.Length, 4); code = coder.GetCode(4, ValidateCodeType.Number); Assert.True(code.IsNumeric()); }
public void CreateValidateCode() { ValidateCoder vc = new ValidateCoder(); string code = vc.CreateValidateCode(4); Session["ValidateCode"] = code; byte[] bytes = vc.CreateValidateGraphic(code); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(bytes); Response.Flush(); Response.End(); }
public ActionResult ValidateCode() { string strCoder = ""; ValidateCoder vCoder = new ValidateCoder() { Height = 32, RandomColor = true, RandomPosition = false, RandomItalic = false }; MemoryStream ms = new MemoryStream(); vCoder.CreateImage(4, out strCoder, ValidateCodeType.NumberAndLetter).Save(ms, ImageFormat.Jpeg); Session["ValidateCode"] = strCoder; return(File(ms.GetBuffer(), @"image/jpeg")); }
public ActionResult VerifyCode() { string code; Bitmap bitmap = new ValidateCoder().CreateImage(4, out code); byte[] bytes = bitmap.ToBytes(); string cacheKey = this.GenVerifyCodeCacheKey(); this.HttpContext.Session.SetString(cacheKey, code); return(this.File(bytes, @"image/Gif")); }
public string VerifyCode() { ValidateCoder coder = new ValidateCoder() { RandomColor = true, RandomItalic = true, RandomLineCount = 7, RandomPointPercent = 10, RandomPosition = true }; Bitmap bitmap = coder.CreateImage(4, out string code); _verifyCodeService.SetCode(code, out string id); return(_verifyCodeService.GetImageString(bitmap, id)); }
public async Task <ApiResult <string> > VerifyCode() { var verifyCodeService = HttpContext.RequestServices.GetService <IVerifyCodeService>(); ValidateCoder coder = new ValidateCoder() { RandomColor = true, RandomItalic = true, RandomLineCount = 7, RandomPointPercent = 10, RandomPosition = true }; Bitmap bitmap = coder.CreateImage(4, out string code); var id = await verifyCodeService.SetCodeAsync(code); return(Result <string>(DefaultStatusCode.Success, verifyCodeService.GetImageString(bitmap, id))); }
/// <summary> /// 发送验证码 /// </summary> /// <param name="codeKey">验证码Key</param> /// <param name="validateType">验证方式</param> /// <param name="codeType">验证码类型</param> /// <param name="sendAction">发送委托</param> /// <returns>业务操作结果</returns> private async Task <OperationResult> SendValidateCode(string codeKey, ValidateType validateType, CodeType codeType, Action <string> sendAction) { codeKey.CheckNotNull("codeKey"); sendAction.CheckNotNull("sendAction"); string validateCode = new ValidateCoder().GetCode(6, ValidateCodeType.Number); var codeEntity = new ValidateCode() { CodeKey = codeKey, Code = validateCode, ValidateType = validateType, CodeType = codeType }; var result = await ValidateCodeRepo.InsertAsync(codeEntity); sendAction(validateCode); return(new OperationResult(OperationResultType.Success, "验证码发送成功", validateCode)); }
public ActionResult VerifyCode() { ValidateCoder coder = new ValidateCoder() { RandomColor = true, RandomItalic = true, RandomPosition = true, RandomLineCount = 5 }; string code; Bitmap bitmap = coder.CreateImage(4, out code); Session[Constants.VerifyCodeSession] = code.ToUpper(); using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Jpeg); return(File(ms.ToArray(), @"image/jpeg")); } }
/// <summary> /// 获取用户注册验证码 /// </summary> /// <param name="phoneNo">手机号</param> /// <param name="codeType"></param> /// <returns>业务操作结果</returns> public async Task <OperationResult> GetSmsValidateCode(string phoneNo, CodeType codeType) { string validateCode = new ValidateCoder().GetCode(6, ValidateCodeType.Number); var codeEntity = new ValidateCode() { PhoneNo = phoneNo, Code = validateCode, CodeType = codeType }; await ValidateCodeRepo.InsertAsync(codeEntity); //发送手机短信 var smsContent = "您本次的验证码为" + validateCode + ",工作人员不会向您索要此验证码,请勿向任何人泄露。[右行养车]"; Sms.Send(phoneNo, 1, smsContent); return(new OperationResult(OperationResultType.Success, "验证码发送成功", validateCode)); }
/// <summary> /// 生成图形验证码信息 /// </summary> /// <param name="captchaKey">图形验证码的key</param> /// <returns></returns> public FileContentResult CreateGraphValidateCode(string captchaKey = AppSettings.UserManagement.UseCaptchaOnRegistration) { //UseCaptchaOnRegistration var length = captchaKey.IndexOf('?'); var key = length <= 0 ? captchaKey : captchaKey.Substring(0, captchaKey.IndexOf('?')); var vCode = new ValidateCoder { FontSize = 20, FontWidth = 14, BgColor = Color.Cornsilk, RandomColor = true }; var code = vCode.GetCode(4); Session[key] = code; var bmp = vCode.CreateImage(code, ValidateCodeType.Number); var ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); return(File(ms.ToArray(), @"image/png")); }
// public IUserContract UserContract { get; set; } /// <summary> /// 获取手机验证码 /// </summary> /// <param name="phoneNo">手机号</param> /// <param name="codeType"></param> /// <returns>业务操作结果</returns> public async Task <OperationResult> GetSmsValidateCode(string phoneNo, CodeType codeType) { var user = UserInfos.Count(x => x.SysUser.PhoneNumber == phoneNo); string validateCode = new ValidateCoder().GetCode(6, ValidateCodeType.Number); var codeEntity = new ValidateCode() { CodeKey = phoneNo, Code = validateCode, ValidateType = ValidateType.手机, CodeType = codeType }; await ValidateCodeRepo.InsertAsync(codeEntity); if (codeType == CodeType.临时密码 || codeType == CodeType.找回密码) { if (user == 0) { return(new OperationResult(OperationResultType.NoChanged, "此帐号还未注册", 0)); } var results = SendMsg53kf.HttpGet((int)codeType, validateCode, phoneNo); if (results.code == 0) { return(new OperationResult(OperationResultType.Success, "验证码发送成功,注意查看短信", 0)); } return(new OperationResult(OperationResultType.Error, "短信系统繁忙,请稍后再试", 0)); } else { if (user > 0) { return(new OperationResult(OperationResultType.NoChanged, "此帐号已注册", 0)); } var result = SendMsg53kf.HttpGet((int)codeType, validateCode, phoneNo); if (result.code == 0) { return(new OperationResult(OperationResultType.Success, "验证码发送成功,注意查看短信", 0)); } return(new OperationResult(OperationResultType.Error, "短信系统繁忙,请稍后再试", 0)); } }