-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSharedKeyStatelessController.cs
More file actions
54 lines (45 loc) · 1.57 KB
/
SharedKeyStatelessController.cs
File metadata and controls
54 lines (45 loc) · 1.57 KB
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
using Edi.Captcha.SampleApp.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Edi.Captcha.SampleApp.Controllers;
public class SharedKeyStatelessController : Controller
{
private readonly IStatelessCaptcha _captcha;
public SharedKeyStatelessController(IEnumerable<IStatelessCaptcha> captchaServices)
{
// Get the last registered IStatelessCaptcha service, which should be SharedKeyStatelessCaptcha
_captcha = captchaServices.Last();
}
public IActionResult Index()
{
return View(new SharedKeyCaptchaModel());
}
[HttpPost]
public IActionResult Index(SharedKeyCaptchaModel model)
{
if (ModelState.IsValid)
{
bool isValidCaptcha = _captcha.Validate(model.CaptchaCode, model.CaptchaToken);
return Content(isValidCaptcha ? "Success - Shared Key Stateless captcha validated!" : "Invalid captcha code");
}
return BadRequest();
}
[Route("get-shared-key-stateless-captcha")]
public IActionResult GetSharedKeyStatelessCaptcha()
{
var result = _captcha.GenerateCaptcha(100, 36);
return Json(new
{
token = result.Token,
imageBase64 = Convert.ToBase64String(result.ImageBytes)
});
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}