Skip to content

Instantly share code, notes, and snippets.

@polatengin
Last active October 3, 2016 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polatengin/555832931701237bb2761616692713f8 to your computer and use it in GitHub Desktop.
Save polatengin/555832931701237bb2761616692713f8 to your computer and use it in GitHub Desktop.
// Asp.Net Core uygulamalarında Google reCaptcha kullanımı
http://www.enginpolat.com/asp-net-core-uygulamalarinda-google-recaptcha-kullanimi
"dependencies": {
"Microsoft.AspNetCore.Mvc": "1.0.1"
}
[HttpPost]
public async Task<IActionResult> Index(string username, string password)
{
var captchaImage = HttpContext.Request.Form["g-recaptcha-response"];
if (string.IsNullOrEmpty(captchaImage))
{
ModelState.AddModelError("captcha", "Captcha doğrulanmamış");
return View();
}
var verified = await CheckCaptcha();
if (!verified)
{
ModelState.AddModelError("captcha", "Captcha yanlış doğrulanmış");
return View();
}
if (ModelState.IsValid)
{
//Herşey doğru
}
return View();
}
private async Task<bool> CheckCaptcha()
{
var postData = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("secret", "6Ld29gYUAAAAAMAOFcvyxvqKHgbHBIwhZ694wOs0"),
new KeyValuePair<string, string>("remoteip", HttpContext.Connection.RemoteIpAddress.ToString()),
new KeyValuePair<string, string>("response", HttpContext.Request.Form["g-recaptcha-response"])
};
var client = new HttpClient();
var response = await client.PostAsync("https://www.google.com/recaptcha/api/siteverify", new FormUrlEncodedContent(postData));
var o = (JObject)JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
return (bool)o["success"];
}
using Microsoft.AspNetCore.Mvc;
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
<!doctype html>
<html lang="tr">
<head>
<title>AspNet Core ve Google reCaptcha</title>
<meta charset="utf-8" />
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form method="POST">
<div>
Kullanıcı Adı : <input type="text" name="username" />
</div>
<div>
Şifre : <input type="password" name="password" />
</div>
<div class="g-recaptcha" data-sitekey="6Ld29gYUAAAAADzK8cpOqjohFsLwxlpJLHYNbveZ"></div>
<div>
<input type="submit" value="Giriş" />
</div>
</form>
</body>
</html>
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes =>
{
routes.MapRoute(name: "Default", template: "{controller=Home}/{action=Index}");
});
}
yo aspnet
// Empty Web Application seçeneğini seçiyoruz
// Projeye asp-net-core-google-recaptcha ismini veriyoruz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment