jsD*_*via 8 asp.net-mvc captcha asp.net-core-mvc asp.net-core
我正在asp.net核心开发asp.net MVC 6应用程序,我想为我的登录页面创建一个验证码.在之前的.net框架中,我使用system.drawing来创建验证码但是因为在.net框架核心中我们没有system.drawing,我该如何实现呢?
一个解决方案是引用完整的.net框架,但这不是我想要的.我想使用核心框架.
另一个是使用.net framework 6和Mvc 5创建一个项目并使用web api来获取验证码图像,但这也不是我想要的.
有另一种解决方案吗?
我在ASP.NET核心应用程序中实现了Recaptcha.在我的登录视图中:
@if (Model.RecaptchaSiteKey.Length > 0)
{
<script src='https://www.google.com/recaptcha/api.js'></script>
}
@if (Model.RecaptchaSiteKey.Length > 0)
{
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="g-recaptcha" data-sitekey="@Model.RecaptchaSiteKey"></div>
@Html.ValidationMessage("recaptchaerror", new { @class = "text-danger" })
</div>
</div>
}
Run Code Online (Sandbox Code Playgroud)
我在控制器上实现了一个扩展方法,因此我可以从我正在使用它的任何控制器轻松验证验证码服务器端.
public static async Task<RecaptchaResponse> ValidateRecaptcha(
this Controller controller,
HttpRequest request,
string secretKey)
{
var response = request.Form["g-recaptcha-response"];
var client = new HttpClient();
string result = await client.GetStringAsync(
string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}",
secretKey,
response)
);
var captchaResponse = JsonConvert.DeserializeObject<RecaptchaResponse>(result);
return captchaResponse;
}
Run Code Online (Sandbox Code Playgroud)
然后我的AccountController中的登录post方法的这个片段使用该扩展方法检查验证码服务器端:
if ((Site.CaptchaOnLogin) && (Site.RecaptchaPublicKey.Length > 0))
{
var recpatchaSecretKey = Site.RecaptchaPrivateKey;
var captchaResponse = await this.ValidateRecaptcha(Request, recpatchaSecretKey);
if (!captchaResponse.Success)
{
ModelState.AddModelError("recaptchaerror", "reCAPTCHA Error occured. Please try again");
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,要在控制器上调用扩展方法,必须使用this关键字.
我目前在多个项目中使用它,所以如果你需要查看更多代码,最简单的是在我的SimpleAuth项目中,但我也在使用它.
我为aspnetcore制作了一个recrapcha包装器,请查看https://www.nuget.org/packages/PaulMiami.AspNetCore.Mvc.Recaptcha并在https://github.com/PaulMiami/reCAPTCHA上找到一些文档/维基/工具入门
简而言之,您需要执行以下操作才能使用它:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddRecaptcha(new RecaptchaOptions
{
SiteKey = Configuration["Recaptcha:SiteKey"],
SecretKey = Configuration["Recaptcha:SecretKey"]
});
}
Run Code Online (Sandbox Code Playgroud)
2.在"_ViewImports.cshtml"文件中添加.
@addTagHelper *, PaulMiami.AspNetCore.Mvc.Recaptcha
Run Code Online (Sandbox Code Playgroud)
3.在你看来.
<form asp-controller="Home" asp-action="Index" method="post">
<recaptcha />
<input type="submit" value="submit" />
</form>
@section Scripts {
<recaptcha-script />
}
Run Code Online (Sandbox Code Playgroud)
4.在你的控制器中.
[ValidateRecaptcha]
[HttpPost]
public IActionResult Index(YourViewModel viewModel)
{
if (ModelState.IsValid)
{
return new OkResult();
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
请按照以下步骤在 Mvc 中创建验证码。
将 CaptchaMvc 库添加到应用程序的参考层,如下所示。
现在像这样创建索引视图。
看法
@using CaptchaMvc.HtmlHelpers
@using MathCaptcha;
@using CaptchaMvc;
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Captcha Example</legend>
@Html.MathCaptcha()
@*@Html.Captcha(3)*@
<input type="submit" value="Send" />
</fieldset>
}
Run Code Online (Sandbox Code Playgroud)
@Html.MathCaptcha() 辅助类然后它将生成一个数学验证码。如果您使用@Html。Captcha(3) 帮助程序类将生成一个 Char CAPTCHA。3 是验证码的长度。
在后操作方法中,我编写了用于验证码验证的代码。
[HttpPost]
public ActionResult Index(string empty)
{
// Code to validate the CAPTCHA image
if (this.IsCaptchaValid("Captcha not valid"))
{
// do your stuff
}
return View();
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
9660 次 |
| 最近记录: |