如何在MVC4应用程序中加载Captcha更快?

时间:2016-12-12 12:27:53

标签: c# asp.net-mvc-4

我在ASP.NET MVC 4应用程序中添加了Captcha。通常,第一次加载验证码需要时间。然后它以完美的速度正常工作。但这是第一次,需要时间。

以下是我的问:有人建议我加载它吗?(通过加载dll或其他任何东西)

DLL: SRVTextToImage

CSTML:

<script>
$(document).ready(function () {
    img = document.getElementById("img_captcha");
    var d = new Date();
    var n = d.getTime();
    debugger;
    @*img.src='@Url.Action("GetCaptchImage", "Contact")?'+n;*@
    $.ajax({
        url: '@Url.Action("GetCaptchImage", "Contact")?'+n,
    }).done(function(response) {
        var filePath = "contact/GetCaptchImage?"+n;
        img.src=filePath;
        document.getElementById('span_loading').style.display = 'none';
       document.getElementById('img_captcha').style.display = 'inline-block';
    });
</script>

<img src="" id="img_captcha" style="width: 200px;display: none;vertical-align: middle;" />

控制器:

[HttpGet]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public FileResult GetCaptchImage()
{
    CaptchaRandomImage CI = new CaptchaRandomImage();
    this.Session["CaptchaImageText"] = CI.GetRandomString(5);
    CI.GenerateImage(this.Session["CaptchaImageText"].ToString(), 300, 75, Color.DarkGray, Color.White);
    MemoryStream stream = new MemoryStream();
    CI.Image.Save(stream, ImageFormat.Png);
    stream.Seek(0, SeekOrigin.Begin);
    return new FileStreamResult(stream, "image/png");
}

1 个答案:

答案 0 :(得分:0)

我有另一个简单的解决方案来整合Recaptcha。尝试使用易于使用和集成的Google Recapture。

转到:Google Recaptcha

注册一个新站点,该站点将为您提供将按以下方式使用的站点密钥:

将此代码段粘贴到HTML模板上的结束标记之前:

<script src='https://www.google.com/recaptcha/api.js'></script>

将此代码段粘贴到您希望显示reCAPTCHA小部件的位置的末尾:

<div class="g-recaptcha" data-sitekey="Your recaptcha Sitekey"></div>

在创建Recaptcha密钥时,不要忘记将您的域添加到密钥设置。