Skip to content

Commit

Permalink
添加使用demo
Browse files Browse the repository at this point in the history
  • Loading branch information
1992w committed Jul 5, 2020
1 parent 20022ca commit ea1a6b4
Show file tree
Hide file tree
Showing 53 changed files with 39,665 additions and 8 deletions.
12 changes: 7 additions & 5 deletions SimpleCaptcha.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30104.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCaptcha", "src\SimpleCaptcha\SimpleCaptcha.csproj", "{0C47EED7-554B-4C81-B3CF-2563422961DE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleCaptcha", "src\SimpleCaptcha\SimpleCaptcha.csproj", "{0C47EED7-554B-4C81-B3CF-2563422961DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCaptcha.Demo", "src\SimpleCaptcha.Demo\SimpleCaptcha.Demo.csproj", "{2EB6B26E-2CB8-4902-9244-15FE5E31E5AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B04D8800-6490-47DF-87E9-84F906A3B815}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B04D8800-6490-47DF-87E9-84F906A3B815}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B04D8800-6490-47DF-87E9-84F906A3B815}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B04D8800-6490-47DF-87E9-84F906A3B815}.Release|Any CPU.Build.0 = Release|Any CPU
{0C47EED7-554B-4C81-B3CF-2563422961DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C47EED7-554B-4C81-B3CF-2563422961DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C47EED7-554B-4C81-B3CF-2563422961DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C47EED7-554B-4C81-B3CF-2563422961DE}.Release|Any CPU.Build.0 = Release|Any CPU
{2EB6B26E-2CB8-4902-9244-15FE5E31E5AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2EB6B26E-2CB8-4902-9244-15FE5E31E5AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2EB6B26E-2CB8-4902-9244-15FE5E31E5AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2EB6B26E-2CB8-4902-9244-15FE5E31E5AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
53 changes: 53 additions & 0 deletions src/SimpleCaptcha.Demo/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SimpleCaptcha.Demo.Models;

namespace SimpleCaptcha.Demo.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ICaptcha _captcha;

public HomeController(ILogger<HomeController> logger, ICaptcha captcha)
{
_logger = logger;
_captcha = captcha;
}

public IActionResult Index()
{
return View();
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

public IActionResult Captcha(string id)
{
var info = _captcha.Generate(id);
var stream = new MemoryStream(info.CaptchaByteData);
return File(stream, "img/png");
}

public IActionResult Validate(string id, string code)
{
var result = _captcha.Validate(id, code);
return Json(new { success = result });
}
}
}
11 changes: 11 additions & 0 deletions src/SimpleCaptcha.Demo/Models/ErrorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace SimpleCaptcha.Demo.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
26 changes: 26 additions & 0 deletions src/SimpleCaptcha.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace SimpleCaptcha.Demo
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
27 changes: 27 additions & 0 deletions src/SimpleCaptcha.Demo/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50605",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SimpleCaptcha.Demo": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
17 changes: 17 additions & 0 deletions src/SimpleCaptcha.Demo/SimpleCaptcha.Demo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SimpleCaptcha\SimpleCaptcha.csproj" />
</ItemGroup>



</Project>
58 changes: 58 additions & 0 deletions src/SimpleCaptcha.Demo/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace SimpleCaptcha.Demo
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddMemoryCache()
.AddSimpleCaptcha(builder =>
{
builder.UseMemoryStore();
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
37 changes: 37 additions & 0 deletions src/SimpleCaptcha.Demo/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@{
ViewData["Title"] = "Home Page";
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<div><input type="text" id="txtCaptcha" /><img id="imgCaptcha" src="/Home/Captcha?id=12345"/><a href="javascript:void(0);" id="changeImg" >换一张</a></div>
<div><button type="button" id="btnValidate">验证</button></div>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

@section Scripts{

<script>
$(function () {
var $imgCaptcha = $('#imgCaptcha')
$('#changeImg').on('click', function () {
$imgCaptcha.attr('src', '/Home/Captcha?id=12345&time='+new Date().getTime())
})
$('#btnValidate').on('click', function () {
var code = $('#txtCaptcha').val()
$.ajax('/Home/Validate?id=12345&code=' + code, {
dataType: "JSON",
success: function (result) {
if (result.success) {
alert('验证码输入正确')
} else {
alert('验证码输入错误')
}
}
})
})
})
</script>
}
6 changes: 6 additions & 0 deletions src/SimpleCaptcha.Demo/Views/Home/Privacy.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>

<p>Use this page to detail your site's privacy policy.</p>
25 changes: 25 additions & 0 deletions src/SimpleCaptcha.Demo/Views/Shared/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@model ErrorViewModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
48 changes: 48 additions & 0 deletions src/SimpleCaptcha.Demo/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - SimpleCaptcha.Demo</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">SimpleCaptcha.Demo</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>

<footer class="border-top footer text-muted">
<div class="container">
&copy; 2020 - SimpleCaptcha.Demo - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
3 changes: 3 additions & 0 deletions src/SimpleCaptcha.Demo/Views/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using SimpleCaptcha.Demo
@using SimpleCaptcha.Demo.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
3 changes: 3 additions & 0 deletions src/SimpleCaptcha.Demo/Views/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}
9 changes: 9 additions & 0 deletions src/SimpleCaptcha.Demo/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
10 changes: 10 additions & 0 deletions src/SimpleCaptcha.Demo/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Loading

0 comments on commit ea1a6b4

Please sign in to comment.