我正在尝试在我的项目中实现reCAPTCHA.AspNetCore 2.1.1 nuget包,所有内容都已配置,我创建了同时包含v2和v3的google验证码网站,因为我需要captcha作品不可见:
标签:SicotX 域:本地主机 版本:v2不可见
标签:本地主机 域:本地主机 版本:V3
我关注了documentation,所以我关注:
Startup.cs,在ConfigureServices中:
services.Configure<RecaptchaSettings>(Configuration.GetSection("RecaptchaSettings"));
services.AddTransient<IRecaptchaService, RecaptchaService>();
在appsettings.json中,我添加了配置:
"RecaptchaSettings": {
"SecretKey": "mysecretkey1",
"SiteKey": "mysitekey1",
"Version": "v2-invisible" }
我在登录表单中添加了@ Html.Recaptcha:
<form asp-area="Identity" asp-controller="Account" asp-action="Login" method="post" class="ffl-form" id="loginForm">
<h3>@Localizer["Ingrese al portal de SicotX"]</h3>
<h6>@Localizer["con sus Credenciales"]</h6>
<hr />
<div class="form-group">
<div class="ffl-wrapper">
<label asp-for="Usuario" class="ffl-label">@Localizer["Usuario"]</label>
<input asp-for="Usuario" class="form-control" autocomplete="off" />
<span asp-validation-for="Usuario" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="ffl-wrapper">
<label asp-for="Contraseña" class="ffl-label">@Localizer["Contraseña"]</label>
<input asp-for="Contraseña" class="form-control" />
<span asp-validation-for="Contraseña" class="text-danger"></span>
</div>
</div>
<div class="form-group">
@Html.Recaptcha(RecaptchaSettings?.Value)
</div>
<div class="form-group text-center">
<button id="recaptcha" type="submit" class="btn btn-primary">@Localizer["Ingresar"] <i class="fa fa-user"></i></button>
</div>
</form>
在帐户控制器登录操作中:
public async Task<IActionResult> Login(LoginViewModel model)
{
var recaptcha = await _recaptcha.Validate(Request);
if (!recaptcha.success)
{
ModelState.AddModelError("Error", _localizer["Hubo un error de validación del Captcha. Por favor intente nuevamente!"].Value);
_logger.LogInformation($"Hubo un error de validación del Captcha.");
} ...
当我运行应用程序时,它会引发异常页面响应:
ValidationException:Recaptcha主机和请求主机不匹配。伪造尝试?
reCAPTCHA.AspNetCore.RecaptchaService.Validate(HttpRequest request, bool antiForgery) in RecaptchaService.cs
SicotX.Areas.Identity.Controllers.AccountController.Login(LoginViewModel model) in AccountController.cs
+
var recaptcha = await _recaptcha.Validate(Request);
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
SicotX.Startup+<>c+<<Configure>b__6_1>d.MoveNext() in Startup.cs
+
await next();
SicotX.Startup+<>c+<<Configure>b__6_0>d.MoveNext() in Startup.cs
+
await next();
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
我将appSettings.json更改为使用V3站点密钥和密钥,但出现相同的异常。
请您帮忙!