Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 44035bd

Browse files
Merge pull request #78 from telerik/captcha-example
chore(demos): add Captcha example
2 parents 8a52341 + ed8e1ec commit 44035bd

File tree

9 files changed

+190
-5
lines changed

9 files changed

+190
-5
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ bin/
1111
packages/
1212
.DS_Store
1313
.vs/
14+
**/wwwroot/shared/Captcha/
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.IO;
4+
using System.Drawing.Imaging;
5+
using Telerik.Web.Captcha;
6+
using Telerik.Examples.Mvc.Models;
7+
using Microsoft.AspNetCore.Http;
8+
9+
namespace Telerik.Examples.Mvc.Controllers.Captcha
10+
{
11+
public class CaptchaOverviewController : Controller
12+
{
13+
protected readonly IWebHostEnvironment HostingEnvironment;
14+
protected readonly string CaptchaPath;
15+
16+
public CaptchaOverviewController(IWebHostEnvironment hostingEnvironment)
17+
{
18+
HostingEnvironment = hostingEnvironment;
19+
CaptchaPath = Path.Combine(hostingEnvironment.WebRootPath, "shared", "Captcha");
20+
21+
if (!Directory.Exists(CaptchaPath))
22+
{
23+
Directory.CreateDirectory(CaptchaPath);
24+
}
25+
}
26+
27+
public IActionResult CaptchaOverview()
28+
{
29+
GenerateNewCaptcha();
30+
return View(new Person());
31+
}
32+
33+
[HttpPost]
34+
public IActionResult Save(Person formData, CaptchaInputModel captchaModel)
35+
{
36+
if (string.IsNullOrEmpty(captchaModel.CaptchaID))
37+
{
38+
GenerateNewCaptcha();
39+
}
40+
else
41+
{
42+
string text = GetCaptchaText(captchaModel.CaptchaID);
43+
44+
if (text == captchaModel.Captcha.ToUpperInvariant())
45+
{
46+
ModelState.Clear();
47+
GenerateNewCaptcha();
48+
}
49+
}
50+
51+
return View("CaptchaOverview", formData);
52+
}
53+
54+
public ActionResult Reset_Events()
55+
{
56+
CaptchaImage newCaptcha = SetCaptchaImage();
57+
58+
return Json(new CaptchaInputModel
59+
{
60+
Captcha = Url.Content("~/shared/Captcha/" + newCaptcha.UniqueId + ".png"),
61+
CaptchaID = newCaptcha.UniqueId
62+
});
63+
}
64+
65+
public ActionResult Validate_Events(CaptchaInputModel model)
66+
{
67+
string text = GetCaptchaText(model.CaptchaID);
68+
69+
return Json(text == model.Captcha.ToUpperInvariant());
70+
}
71+
72+
private void GenerateNewCaptcha()
73+
{
74+
CaptchaImage captchaImage = SetCaptchaImage();
75+
76+
ViewData["Captcha"] = Url.Content("~/shared/Captcha/" + captchaImage.UniqueId + ".png");
77+
ViewData["CaptchaID"] = captchaImage.UniqueId;
78+
}
79+
80+
private string GetCaptchaText(string captchaId)
81+
{
82+
string text = HttpContext.Session.GetString("captcha_" + captchaId);
83+
84+
return text;
85+
}
86+
87+
private CaptchaImage SetCaptchaImage()
88+
{
89+
CaptchaImage newCaptcha = CaptchaHelper.GetNewCaptcha();
90+
91+
MemoryStream audio = CaptchaHelper.SpeakText(newCaptcha);
92+
using (FileStream file = new FileStream(Path.Combine(CaptchaPath, newCaptcha.UniqueId + ".wav"), FileMode.Create, FileAccess.Write))
93+
{
94+
audio.WriteTo(file);
95+
}
96+
97+
var image = CaptchaHelper.RenderCaptcha(newCaptcha);
98+
image.Save(Path.Combine(CaptchaPath, newCaptcha.UniqueId + ".png"), ImageFormat.Png);
99+
100+
HttpContext.Session.SetString("captcha_" + newCaptcha.UniqueId, newCaptcha.Text);
101+
102+
return newCaptcha;
103+
}
104+
}
105+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Telerik.Examples.Mvc.Models
2+
{
3+
public class CaptchaInputModel
4+
{
5+
private string _captchaValue;
6+
public string Captcha
7+
{
8+
get
9+
{
10+
return string.IsNullOrEmpty(_captchaValue) ? "" : _captchaValue;
11+
}
12+
set
13+
{
14+
_captchaValue = value;
15+
}
16+
}
17+
public string CaptchaID { get; set; }
18+
}
19+
}

‎Telerik.Examples.Mvc/Telerik.Examples.Mvc/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
builder.Services.Configure<RazorViewEngineOptions>(options =>
5050
{
51+
options.ViewLocationFormats.Add("/Views/Captcha/{0}" + RazorViewEngine.ViewExtension);
5152
options.ViewLocationFormats.Add("/Views/Grid/{0}" + RazorViewEngine.ViewExtension);
5253
options.ViewLocationFormats.Add("/Views/ImageEditor/{0}" + RazorViewEngine.ViewExtension);
5354
options.ViewLocationFormats.Add("/Views/Editor/{0}" + RazorViewEngine.ViewExtension);
@@ -79,6 +80,12 @@
7980
options.PayloadSerializerOptions.PropertyNamingPolicy = null;
8081
});
8182

83+
builder.Services
84+
.AddDistributedMemoryCache()
85+
.AddSession(opts => {
86+
opts.Cookie.IsEssential = true;
87+
});
88+
8289
builder.Services.Configure<IISServerOptions>(options =>
8390
{
8491
options.AllowSynchronousIO = true;
@@ -103,6 +110,7 @@
103110

104111
app.UseAuthentication();
105112
app.UseAuthorization();
113+
app.UseSession();
106114

107115
app.UseEndpoints(endpoints =>
108116
{

‎Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.5" />
1111
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.5" />
1212
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.5" />
13-
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.5" />
13+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.13" />
1414
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.5" />
1515
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.2.0" />
1616
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
@@ -21,7 +21,8 @@
2121
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
2222
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.6" />
2323
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
24-
<PackageReference Include="Telerik.UI.for.AspNet.Core.Trial" Version="20231425" />
24+
<PackageReference Include="Telerik.UI.for.AspNet.Core.Trial" Version="2023年3月11日14" />
25+
<PackageReference Include="Telerik.Web.Captcha.Trial" Version="1.1.2" />
2526
</ItemGroup>
2627

2728
<ItemGroup>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
@model Person
2+
3+
@(Html.Kendo().Form<Person>()
4+
.Name("exampleForm")
5+
.HtmlAttributes(new { action = "Save", controller = "CaptchaOverview", method = "POST" })
6+
.Validatable(v =>
7+
{
8+
v.ValidateOnBlur(true);
9+
v.ValidationSummary(vs => vs.Enable(false));
10+
})
11+
.FormData(Model)
12+
.Items(i =>
13+
{
14+
i.Add()
15+
.Field(f => f.Name)
16+
.InputHtmlAttributes(new { required = "required" })
17+
.Label(l => l.Text("Name"));
18+
i.Add()
19+
.Field(f => f.BirthDate)
20+
.Label(l => l.Text("Date of birth:"));
21+
i.Add().Field("Captcha")
22+
.Hint("Type the characters you see in the picture above.")
23+
.Label(l => l.Text("Anti-bot validation"))
24+
.Editor(ed => ed.Captcha()
25+
.CaptchaImage((string)ViewData["Captcha"])
26+
.CaptchaId((string)ViewData["CaptchaID"])
27+
.DataCaptchaField("Captcha")
28+
.DataCaptchaIdField("CaptchaID")
29+
.Handler(handler => handler.Action("Reset_Events", "CaptchaOverview"))
30+
.AudioHandlerFunction("audioHandler")
31+
.ValidationHandler(handler => handler.Action("Validate_Events", "CaptchaOverview"))
32+
);
33+
})
34+
)
35+
36+
<script>
37+
function audioHandler(args) {
38+
args.success("../shared/Captcha/" + args.data.CaptchaID + ".wav");
39+
}
40+
</script>
41+
42+
43+
<style>
44+
form {
45+
display: flex;
46+
flex-direction: column;
47+
width: 500px;
48+
margin: auto;
49+
}
50+
</style>
51+

‎Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Shared/_Layout.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>@ViewData["Title"] - Telerik.Examples.Mvc</title>
77
@{
8-
var kendoVersion = "2023.1.425";
8+
var kendoVersion = "2023.3.1114";
99
}
10-
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/themes/6.3.0/default/default-ocean-blue.css">
10+
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/themes/7.0.2/default/default-ocean-blue.css">
1111

12-
<script src="https://kendo.cdn.telerik.com/@kendoVersion/js/jquery.min.js"></script>
12+
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
1313
<script src="https://kendo.cdn.telerik.com/@kendoVersion/js/jszip.min.js"></script>
1414
<script src="https://kendo.cdn.telerik.com/@kendoVersion/js/kendo.all.min.js"></script>
1515
<script src="https://kendo.cdn.telerik.com/@kendoVersion/js/kendo.aspnetmvc.min.js"></script>
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /