My ultimate goal is to get reCaptcha to work in my Razor app, but I am stuck. I've gotten to the point where I can get Response Headers back, but have no idea how to get the actual JSON response body back. Let me know if you need more info.
In Startup.cs ----------------------------------
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
In Models > Voter.cs ----------------------------------
using System.ComponentModel.DataAnnotations;
namespace rethianIdeas.Models
{
public class Voter
{
[Required(ErrorMessage = "Data is required.")]
public string reCaptchaPost { get; set; }
public string Result { get; set; }
}
}
In Vote2.cshtml.cs ----------------------------------
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using rethianIdeas.Models;
using System.ComponentModel.DataAnnotations;
namespace rethianIdeas.Pages.Ideas
{
public class Vote2Model : PageModel
{
[BindProperty]
public Voter reCaptchaTest { get; set; }
[BindProperty(Name = "g-recaptcha-response")]
[Required(ErrorMessage = "No One Beats Reacaptcha!")]
public string reCaptchaG { get; set; }
public IActionResult OnGet()
{
if (reCaptchaTest == null)
{
reCaptchaTest = new Voter();
}
return Page();
}
private readonly IHttpClientFactory _clientFactory;
public Vote2Model(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public IActionResult OnPost()
{
if (ModelState.IsValid)
{
string URL = "https://www.google.com/recaptcha/api/siteverify?secret=6LcUFJUUAAAAADvuNOTGONNAGETITiFgNNG0sxBg&response=";
string Response_String = reCaptchaG;
string GetRequest = URL + Response_String;
var reCaptchaRequest = new HttpRequestMessage(HttpMethod.Get, GetRequest);
reCaptchaRequest.Headers.Add("Accept", "application/json");
var Client = _clientFactory.CreateClient().SendAsync(reCaptchaRequest);
reCaptchaTest.Result = Client.Result.ToString();
}
return Page();
}
}
}
In Vote2.cshtml ----------------------------------
@page
@model rethianIdeas.Pages.Ideas.Vote2Model
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form method="post">
<div asp-validation-summary="All"></div>
<div>
<div>
<label asp-for="@Model.reCaptchaTest.reCaptchaPost"></label>
<input asp-for="@Model.reCaptchaTest.reCaptchaPost" />
<span asp-validation-for="@Model.reCaptchaTest.reCaptchaPost"></span>
</div>
</div>
<div class="g-recaptcha" data-sitekey="6LcUFJUUAAAAANOTGETTINGIT0SYj77GZHEhz73p0Q6_m"></div>
<div>
<input type="submit" value="Submit">
</div>
<div><label>Result: @Model.reCaptchaTest.Result</label></div>
</form>
Returns ----------------------------------
Result: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers: { Date: Fri, 08 Mar 2019 06:02:16 GMT Cache-Control: max-age=0, private X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Server: GSE Alt-Svc: quic=":443"; ma=2592000; v="46,44,43,39" Accept-Ranges: none Vary: Accept-Encoding Transfer-Encoding: chunked Content-Type: application/json; charset=utf-8 Expires: Fri, 08 Mar 2019 06:02:16 GMT }
I simply cannot figure out how to get the body of the request out of this thing. Please Help.
You are calling an asynchronous method but not waiting ( await
) for it to return.
This should get you going down the right path.
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
string URL = "https://www.google.com/recaptcha/api/siteverify?secret=***&response=";
string Response_String = reCaptchaG;
string GetRequest = URL + Response_String;
var reCaptchaRequest = new HttpRequestMessage(HttpMethod.Get, GetRequest);
reCaptchaRequest.Headers.Add("Accept", "application/json");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(reCaptchaRequest);
if (response.IsSuccessStatusCode)
{
reCaptchaTest.Result = await response.Content.ReadAsStringAsync();
}
else
{
// Errors, do something...
}
}
return Page();
}
You can reference the ASP.NET Core Http.Client docs for more info. Also, here is a blog post covering using ASP.NET Core and reCaptcha.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.