/// <summary> /// Generates the form fields' HTML for spam prevention. Use inside of an ASP.NET MVC form. /// </summary> /// <param name="htmlHelper">The view's HTML helper.</param> /// <param name="fallbackMessage">An optional message to display above the CAPTCHA when it is displayed as a fallback.</param> /// <returns>The spam prevention form fields' HTML</returns> public static IHtmlString SpamPreventionFields( this HtmlHelper htmlHelper, string fallbackMessage = null) { ICaptchaGenerator captchaGenerator = null; if (DependencyResolver.Current != null) captchaGenerator = DependencyResolver.Current.GetService<ICaptchaGenerator>(); if (captchaGenerator == null) captchaGenerator = new ReCaptchaGenerator(); var useCaptcha = htmlHelper.ViewData.ModelState.ContainsKey(Const.ModelStateKey); if (useCaptcha) return captchaGenerator.Generate(htmlHelper, fallbackMessage); return htmlHelper.Hidden(Const.NoCaptchaChallengeField, Guid.NewGuid().ToString("N")); }
/// <summary> /// Generates the form fields' HTML for spam prevention. Use inside of an ASP.NET MVC form. /// </summary> /// <param name="htmlHelper">The view's HTML helper.</param> /// <param name="fallbackMessage">An optional message to display above the CAPTCHA when it is displayed as a fallback.</param> /// <returns>The spam prevention form fields' HTML</returns> public static IHtmlString SpamPreventionFields( this HtmlHelper htmlHelper, string fallbackMessage = null) { ICaptchaGenerator captchaGenerator = null; if (DependencyResolver.Current != null) { captchaGenerator = DependencyResolver.Current.GetService <ICaptchaGenerator>(); } if (captchaGenerator == null) { captchaGenerator = new ReCaptchaGenerator(); } var useCaptcha = htmlHelper.ViewData.ModelState.ContainsKey(Const.ModelStateKey); if (useCaptcha) { return(captchaGenerator.Generate(htmlHelper, fallbackMessage)); } //Session hacks var session = htmlHelper.ViewContext.HttpContext.Session; var guidKey = "_" + Const.NoCaptchaChallengeField; string guid; if (session[guidKey] != null) { guid = (string)session[guidKey]; } else { guid = Guid.NewGuid().ToString("N"); } //set guid into session for using again later session[guidKey] = guid; //-- return(htmlHelper.Hidden(Const.NoCaptchaChallengeField, guid)); }
/// <summary> /// Generates the form fields' HTML for spam prevention. Use inside of an ASP.NET MVC form. /// </summary> /// <param name="htmlHelper">The view's HTML helper.</param> /// <param name="fallbackMessage">An optional message to display above the CAPTCHA when it is displayed as a fallback.</param> /// <returns>The spam prevention form fields' HTML</returns> public static IHtmlString SpamPreventionFields( this HtmlHelper htmlHelper, string fallbackMessage = null) { ICaptchaGenerator captchaGenerator = null; if (DependencyResolver.Current != null) { captchaGenerator = DependencyResolver.Current.GetService <ICaptchaGenerator>(); } if (captchaGenerator == null) { captchaGenerator = new ReCaptchaGenerator(); } var useCaptcha = htmlHelper.ViewData.ModelState.ContainsKey(Const.ModelStateKey); if (useCaptcha) { return(captchaGenerator.Generate(htmlHelper, fallbackMessage)); } return(htmlHelper.Hidden(Const.NoCaptchaChallengeField, Guid.NewGuid().ToString("N"))); }