forked from VahidN/DNTCaptcha.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeController.cs
71 lines (60 loc) · 2.29 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using DNTCaptcha.Core;
using DNTCaptcha.TestWebApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace DNTCaptcha.TestWebApp.Controllers;
public class HomeController : Controller
{
private readonly DNTCaptchaOptions _captchaOptions;
private readonly IDNTCaptchaValidatorService _validatorService;
public HomeController(
IDNTCaptchaValidatorService validatorService,
IOptions<DNTCaptchaOptions> options
)
{
_validatorService = validatorService;
_captchaOptions = options == null ? throw new ArgumentNullException(nameof(options)) : options.Value;
}
public IActionResult Index() => View();
// You can use it as an ActionFilter.
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateDNTCaptcha(ErrorMessage = "Please enter the security code as a number.")]
public IActionResult Index([FromForm] AccountViewModel data)
{
if (ModelState.IsValid) // If `ValidateDNTCaptcha` fails, it will set a `ModelState.AddModelError`.
{
//TODO: Save data
return RedirectToAction(nameof(Thanks), new { name = data.Username });
}
return View();
}
// Or you can use the `IDNTCaptchaValidatorService` directly.
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Login2([FromForm] AccountViewModel data)
{
if (!_validatorService.HasRequestValidCaptchaEntry())
{
ModelState.AddModelError(_captchaOptions.CaptchaComponent.CaptchaInputName,
"Please enter the security code as a number.");
return View(nameof(Index));
}
//TODO: Save data
return RedirectToAction(nameof(Thanks), new { name = data.Username });
}
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateDNTCaptcha(ErrorMessage = "Please enter the security code as a number.")]
public IActionResult Login3([FromForm] AccountViewModel data) // For Ajax Forms
{
if (ModelState.IsValid) // If `ValidateDNTCaptcha` fails, it will set a `ModelState.AddModelError`.
{
//TODO: Save data
return Ok();
}
return BadRequest(ModelState);
}
public IActionResult Thanks(string name) => View(nameof(Thanks), name);
}