private static string MakeRandomSolution(CaptchaSettingsPart settings) { Random rng = new Random(); char[] buf = new char[settings.TotalChars]; for (int i = 0; i < settings.TotalChars; i++) { Func <char> RndDigit = () => (char)('0' + rng.Next(10)); Func <char> RndLetter = () => (char)('a' + rng.Next(26)); if (settings.IncDigits && settings.IncLetters) { buf[i] = rng.Next(11) > 5 ? RndDigit() : RndLetter(); } else if (settings.IncDigits) { buf[i] = RndDigit(); } else { buf[i] = RndLetter(); } } return(new string(buf)); }
public bool IsCaptchaValid(string captchaChallengeValue, string captchaResponseValue, string userHostAddress) { _captchaSettings = _orchardServices.WorkContext.CurrentSite.As <CaptchaSettingsPart>(); var captchaValidtor = new Recaptcha.RecaptchaValidator { PrivateKey = _captchaSettings.PrivateKey, RemoteIP = userHostAddress, Challenge = captchaChallengeValue, Response = captchaResponseValue }; var recaptchaResponse = captchaValidtor.Validate(); // this will push the result value into a parameter in our Action return(recaptchaResponse.IsValid); }
private GraphicsPath DeformPath(GraphicsPath path, CaptchaSettingsPart settings) { Double xFreq = 2 * Math.PI / settings.ImageWidth; Double yFreq = 2 * Math.PI / settings.ImageHeight; PointF[] deformed = new PointF[path.PathPoints.Length]; Random rng = new Random(); Double xSeed = rng.NextDouble() * 2 * Math.PI; Double ySeed = rng.NextDouble() * 2 * Math.PI; for (int i = 0; i < path.PathPoints.Length; i++) { PointF original = path.PathPoints[i]; Double val = xFreq * original.X + yFreq * original.Y; int xOffset = (int)(settings.XAmp * Math.Sin(val + xSeed)); int yOffset = (int)(settings.YAmp * Math.Sin(val + ySeed)); deformed[i] = new PointF(original.X + xOffset, original.Y + yOffset); } return(new GraphicsPath(deformed, path.PathTypes)); }
public string GenerateCaptcha() { _captchaSettings = _orchardServices.WorkContext.CurrentSite.As <CaptchaSettingsPart>(); var CurrentCulture = _orchardServices.WorkContext.CurrentCulture; if (_captchaSettings == null) { return("CAPTCHA Part Record Was Not Found"); } var captchaControl = new Recaptcha.RecaptchaControl { ID = "recaptcha", PublicKey = _captchaSettings.PublicKey, PrivateKey = _captchaSettings.PrivateKey, Theme = _captchaSettings.Theme, Language = CurrentCulture.Substring(0, 2) }; string captchaMarkup; if (captchaControl.Theme.ToLower() != "custom") { var htmlWriter = new HtmlTextWriter(new StringWriter()); captchaControl.RenderControl(htmlWriter); captchaMarkup = htmlWriter.InnerWriter.ToString(); } else { if (_captchaSettings.CustomCaptchaMarkup != string.Empty) { captchaMarkup = _captchaSettings.CustomCaptchaMarkup; } else { captchaMarkup = "<script type=\"text/javascript\">var RecaptchaOptions = {theme: 'custom', custom_theme_widget: 'recaptcha_widget', lang: '" + CurrentCulture.Substring(0, 2) + "' };</script>"; captchaMarkup += "<div id=\"recaptcha_widget\" style=\"display:none\"><div id=\"recaptcha_image\"></div><div class=\"recaptcha_only_if_incorrect_sol\">" + T("Incorrect please try again") + "</div><span class=\"recaptcha_only_if_image\">" + T("Enter the words above") + "</span><span class=\"recaptcha_only_if_audio\">" + T("Enter the numbers you hear:") + "</span><input type=\"text\" id=\"recaptcha_response_field\" name=\"recaptcha_response_field\" /><div><a href=\"javascript:Recaptcha.reload()\">" + T("Get another CAPTCHA") + "</a></div><div class=\"recaptcha_only_if_image\"><a href=\"javascript:Recaptcha.switch_type('audio')\">" + @T("Get an audio CAPTCHA") + "</a></div><div class=\"recaptcha_only_if_audio\"><a href=\"javascript:Recaptcha.switch_type('image')\">" + @T("Get an image CAPTCHA") + "</a></div><div><a href=\"javascript:Recaptcha.showhelp()\">Help</a></div></div><script type=\"text/javascript\" src=\"http://www.google.com/recaptcha/api/challenge?k=" + captchaControl.PublicKey + "\"></script><noscript><iframe src=\"http://www.google.com/recaptcha/api/noscript?k=" + captchaControl.PublicKey + "\" height=\"300\" width=\"500\" frameborder=\"0\"></iframe><br><textarea name=\"recaptcha_challenge_field\" rows=\"3\" cols=\"40\"></textarea><input type=\"hidden\" name=\"recaptcha_response_field\" value=\"manual_challenge\"></noscript>"; } } return(captchaMarkup); }
public FileContentResult GetCaptchaImage(string challengeGuid, int width, int height) { // Retrieve the solution text from Session[] var captcha = HttpContext.Session[CaptchaServiceConstants.SESSION_KEY_PREFIX + challengeGuid] as CaptchaViewModel; if (captcha != null) { //var settings = _orchardServices.WorkContext.CurrentSite.As<CaptchaSettingsPart>(); var settings = new CaptchaSettingsPart(); settings.ImageWidth = width; settings.ImageHeight = height; if (settings.Foreground != _foregroundArgb) { lock (_padLock) { if (settings.Foreground != _foregroundArgb) { _foregroundArgb = settings.Foreground; _foreground.Dispose(); _foreground = new SolidBrush(Color.FromArgb(_foregroundArgb)); } } } if (settings.Background != _backgroundArgb) { _backgroundArgb = settings.Background; _background = Color.FromArgb(_backgroundArgb); } // Make a blank canvas to render the CAPTCHA on using (Bitmap bmp = new Bitmap(settings.ImageWidth, settings.ImageHeight)) using (Graphics g = Graphics.FromImage(bmp)) using (Font font = new Font(settings.ImageFont, 1f)) { g.Clear(_background); // Perform trial rendering to determine best font size SizeF finalSize; SizeF testSize = g.MeasureString(captcha.Value, font); float bestFontSize = Math.Min(settings.ImageWidth / testSize.Width, settings.ImageHeight / testSize.Height) * 0.95f; using (Font finalFont = new Font(settings.ImageFont, bestFontSize)) { finalSize = g.MeasureString(captcha.Value, finalFont); } // Get a path representing the text centered on the canvas g.PageUnit = GraphicsUnit.Point; PointF textTopLeft = new PointF((settings.ImageWidth - finalSize.Width) / 2, (settings.ImageHeight - finalSize.Height) / 2); using (GraphicsPath path = new GraphicsPath()) { path.AddString(captcha.Value, new FontFamily(settings.ImageFont), 0, bestFontSize, textTopLeft, StringFormat.GenericDefault); // Render the path to the bitmap g.SmoothingMode = SmoothingMode.HighQuality; g.FillPath(_foreground, DeformPath(path, settings)); g.Flush(); // Send the image to the response stream in PNG format //.ContentType = "image/png"; //using (var memoryStream = new MemoryStream()) //{ // bmp.Save(memoryStream, ImageFormat.Png); // memoryStream.WriteTo(Response.OutputStream); // Logger.Log(LogLevel.Debug, null, "111"); //} // Send the image to the response stream in PNG format //Response.ContentType = "image/png"; using (var memoryStream = new MemoryStream()) { //Response.Clear(); bmp.Save(memoryStream, ImageFormat.Png); return(new FileContentResult(memoryStream.GetBuffer(), "image/png")); } } } } return(null); }