-
-
Save polatengin/555832931701237bb2761616692713f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Asp.Net Core uygulamalarında Google reCaptcha kullanımı | |
http://www.enginpolat.com/asp-net-core-uygulamalarinda-google-recaptcha-kullanimi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"dependencies": { | |
"Microsoft.AspNetCore.Mvc": "1.0.1" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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"]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
public class HomeController : Controller | |
{ | |
public IActionResult Index() | |
{ | |
return View(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
} | |
public void Configure(IApplicationBuilder app) | |
{ | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute(name: "Default", template: "{controller=Home}/{action=Index}"); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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