文档半岛外围网上直营>>telerik中文文档>>验证码提供方
验证码提供方
立即下载Kendo UI for jQuery
本文解释了如何为Kendo UI Captcha设置服务端提供者,提供程序通过帮助程序类和方法生成并验证验证码。
下面的列表提供了关于重置、音频和验证操作的默认请求和响应的信息。
-
reset——向服务器发出GET请求以生成新的Captcha图像,响应包含新的验证码图像(在下面的代码片段中—一个返回新图像的端点)和captchald。
{captcha: "/kendo-ui-dev/captcha/image?captchaid=36967dc7-0ae1-4175-9bb7-9d7f34409889", captchaId: "36967dc7-0ae1-4175-9bb7-9d7f34409889"} -
audio——发出一个包含验证码的GET请求,来自服务器的响应不包含任何参数而是验证码图像的音频表示。
-
validate——生成一个包含captchald和用户在输入字段中键入的文本的GET请求,如果输入文本与图像文本匹配则响应为真,如果输入文本与图像文本不匹配则响应为假。
设置
服务端Kendo UI Captcha提供程序与Telerik.Web .Captcha NuGet包一起提供。
安装Telerik.Web.Captcha NuGet包:
- 右键单击项目并选择Manage NuGet Packages。
- 确保私有Telerik UI NuGet已配置。
- 搜索并安装Telerik.Web.Captcha NuGet包。
在c#后端文件或控制器中,添加对以下命名空间的引用:
using System; using System.Drawing.Imaging; using System.IO; using System.Web.Mvc; using Telerik.Web.Captcha;
下面的代码片段显示了控制器的基本设置和验证码的必要端点:
using System.Drawing.Imaging;
using System.IO;
using System.Web.Mvc;
using Telerik.Web.Captcha;
namespace Kendo.Controllers
{
public class CaptchaController : Controller
{
public ActionResult Reset()
{
CaptchaImage newCaptcha = CaptchaHelper.GetNewCaptcha();
Session["captcha" + newCaptcha.UniqueId] = newCaptcha;
return Json(new
{
captcha = Url.Action("image", "captcha", new { captchaId = newCaptcha.UniqueId }),
captchaId = newCaptcha.UniqueId
}, JsonRequestBehavior.AllowGet);
}
public ActionResult Image(string captchaId)
{
CaptchaImage captcha = (CaptchaImage)Session["captcha" + captchaId];
var image = CaptchaHelper.RenderCaptcha(captcha);
byte[] bmpBytes;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
bmpBytes = ms.ToArray();
}
return File(bmpBytes, "image/png");
}
public ActionResult Audio(string captchaId)
{
CaptchaImage captcha = (CaptchaImage)Session["captcha" + captchaId];
byte[] bmpBytes;
using (MemoryStream audio = CaptchaHelper.SpeakText(captcha))
{
bmpBytes = audio.ToArray();
}
return File(bmpBytes, "audio/wav");
}
public ActionResult Validate(string captchaId, string captcha)
{
CaptchaImage captchaImage = (CaptchaImage)Session["captcha" + captchaId];
return Json(CaptchaHelper.Validate(captchaImage, captcha.ToUpperInvariant()), JsonRequestBehavior.AllowGet);
}
}
}

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线