【发布时间】:2022-01-01 14:16:37
【问题描述】:
我有一个 WebApi 项目。由于项目需要,注册用户时需要一个图形验证码。有什么有用的建议吗?另外,如何结合Redis将其存储在Redis中。像这样,但是我需要的是数字和4位数字。谢谢您的时间。 enter image description here
【问题讨论】:
标签: asp.net-core asp.net-web-api
我有一个 WebApi 项目。由于项目需要,注册用户时需要一个图形验证码。有什么有用的建议吗?另外,如何结合Redis将其存储在Redis中。像这样,但是我需要的是数字和4位数字。谢谢您的时间。 enter image description here
【问题讨论】:
标签: asp.net-core asp.net-web-api
我根据你的需要写了一个Demo,你可以参考一下。
首先我使用Bitmap库生成图形验证码,代码如下:
public class VerifyCodeHelper
{
public static Bitmap CreateVerifyCode(out string code)
{
//Create a Bitmap object and draw
Bitmap bitmap = new Bitmap(200, 60);
Graphics graph = Graphics.FromImage(bitmap);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
string letters = "0123456789";
StringBuilder sb = new StringBuilder();
//Add random 4 numbers
for (int x = 0; x < 4; x++)
{
string letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
sb.Append(letter);
graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
}
code = sb.ToString();
//Confuse the background
Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
for (int x = 0; x < 6; x++)
graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
return bitmap;
}
}
然后创建控制器调用API生成图形验证码。然后配置Redis:
Package Manager:
Install-Package Microsoft.Extensions.Caching.StackExchangeRedis
要集成 Redis 功能,请在启动文件中配置“AddStackExchangeRedisCache()”服务。
services.AddStackExchangeRedisCache(options => {
options.Configuration = "localhost:6379";
});
然后将 IDistributedCache 接口注入我们的端点控制器。
然后将_distributedCache.SetString接收到的4位随机数存入数据库,全部代码如下:
[ApiController]
public class VerifyCodeController : Controller
{
private readonly IDistributedCache _distributedCache;
public VerifyCodeController(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}
[Route("get_captcha")]
public Object VerifyCode()
{
string code = "";
Bitmap bitmap = VerifyCodeHelper.CreateVerifyCode(out code);
_distributedCache.SetString("code",code);
base.HttpContext.Session.SetString("CheckCode",code);
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Gif);
return File(stream.ToArray(), "image/gif");
}
}
整体 Startup.cs 配置如下。如果调用 API 时出错,请记得仔细检查这些配置:
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.AddStackExchangeRedisCache(options => {
options.Configuration = "localhost:6379";
});
services.AddControllers();
services.AddDistributedMemoryCache();
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(1);
});
// Add framework services.
services.AddMvc();
}
// 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();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseStaticFiles();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
最终结果:
生成的图形验证码:
Redis 数据:
【讨论】: