add 验证码demo示例

This commit is contained in:
luoyunchong 2020-09-04 01:37:34 +08:00
parent 35fa68cffc
commit 8a4066a40e
6 changed files with 80 additions and 14 deletions

View File

@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.5.1" />
</ItemGroup>
<ItemGroup>

View File

@ -99,9 +99,14 @@ namespace Basic
app.UseSwagger(c =>
{
});
app.UseSwaggerUI(c =>
{
c.RoutePrefix = ""; // serve the UI at root
c.SwaggerEndpoint("/v1/api-docs", "V1 Docs");
c.SwaggerEndpoint("/gp/api-docs", "되쩌친욥");
});
app.UseKnife4UI(c =>
{
//c.RoutePrefix = ""; // serve the UI at root

View File

@ -4,6 +4,8 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using IdentityServer4;
using IdentityServer4.Test;
using NCaptcha.Abstractions;
using NCaptcha.AspNetCore.Extensions;
namespace OAuth2Integration.AuthServer.Controllers
{
@ -26,8 +28,9 @@ namespace OAuth2Integration.AuthServer.Controllers
return View("/AuthServer/Views/Login.cshtml", viewModel);
}
[HttpPost("login")]
public async Task<IActionResult> Login([FromForm]LoginViewModel viewModel)
public async Task<IActionResult> Login([FromForm] LoginViewModel viewModel)
{
if (!_userStore.ValidateCredentials(viewModel.Username, viewModel.Password))
{
@ -43,6 +46,7 @@ namespace OAuth2Integration.AuthServer.Controllers
return Redirect(viewModel.ReturnUrl);
}
}
public class LoginViewModel

View File

@ -1,17 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="3.0.2" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="3.0.2" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
<PackageReference Include="NCaptcha.AspNetCore.SessionImages" Version="0.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.5.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.5.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\IGeekFan.AspNetCore.Knife4jUI\IGeekFan.AspNetCore.Knife4jUI.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NCaptcha.Abstractions;
using NCaptcha.AspNetCore.Extensions;
namespace OAuth2Integration.ResourceServer.Controllers
{
[Route("captcha")]
public class CaptchaController : Controller
{
private readonly ICaptchaGenerator _captchaGenerator;
public CaptchaController(ICaptchaGenerator captchaGenerator)
{
_captchaGenerator = captchaGenerator;
}
[Produces("image/gif", Type = typeof(FileContentResult))]
[HttpGet]
public async Task<IActionResult> OnGetCaptchaAsync() => await _captchaGenerator.GetCaptchaFileResultAsync();
}
}

View File

@ -7,6 +7,9 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using NCaptcha.AspNetCore.SessionImages;
using IGeekFan.AspNetCore.Knife4jUI;
using Microsoft.AspNetCore.Mvc.Controllers;
namespace OAuth2Integration
{
@ -50,6 +53,9 @@ namespace OAuth2Integration
services.AddControllersWithViews();
services.AddSession();
services.AddSessionBasedImageCaptcha();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "Test API V1" });
@ -73,6 +79,12 @@ namespace OAuth2Integration
}
});
c.CustomOperationIds(apiDesc =>
{
var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
return controllerAction.ControllerName + "-" + controllerAction.ActionName;
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
@ -97,6 +109,8 @@ namespace OAuth2Integration
app.UseDeveloperExceptionPage();
}
app.UseSession();
app.Map("/auth-server", authServer =>
{
authServer.UseRouting();
@ -126,8 +140,22 @@ namespace OAuth2Integration
});
resourceServer.UseSwagger();
app.UseKnife4UI(c =>
{
c.RoutePrefix = ""; //http://localhost:5000/index.html#/home
c.SwaggerEndpoint("/resource-server/swagger/v1/swagger.json", "My API V1");
c.EnableDeepLinking();
c.OAuthClientId("test-id");
c.OAuthClientSecret("test-secret");
c.OAuthAppName("test-app");
c.OAuthScopeSeparator(" ");
c.OAuthUsePkce();
});
resourceServer.UseSwaggerUI(c =>
{
//http://localhost:5000/resource-server/swagger/index.html
c.SwaggerEndpoint("/resource-server/swagger/v1/swagger.json", "My API V1");
c.EnableDeepLinking();