ASP.NET Core在Docker下面生成简易验证码

背景

验证码这个功能是十分常见的,各大系统的登陆页面都会有。今天介绍一下最为普通的验证码。前端

不管最终给到前端的是图片格式的验证码仍是base64格式的验证码,其实都离不开这样的一步操做,都要先在后台生成一个图片。git

就我的经验来讲,早期的.NET Core想在Linux/Docker下面搞图片这些仍是相对麻烦一些的,首先是组件这一块,其次是依赖这一块。github

不过,如今方便多了。docker

下面就基于ImageSharp这个组件来实践一下。app

准备工做

  1. 安装相应的nuget包
<ItemGroup>
    <PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />
    <PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta0009" />
    <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0007" />
</ItemGroup>

须要注意的是,ImageSharp目前还不是正式版。dom

  1. 准备一下字体文件

示例用的是comic.ttf,这个只有240kb,算是比较小的了。字体

这里选一个小一点的字体是由于不想让打包好的镜像太大,若是各位大佬有更小体积的字体能够告诉我一下哈。ui

编码实现

首先是生成随机数,应该不用说太多,若是先生成完成不同的数字或字母,自由控制就好。编码

private static string GenCode(int num)
{
    var code = string.Empty;
    var r = new Random();

    for (int i = 0; i < num; i++)
    {
        code += Chars[r.Next(Chars.Length)].ToString();
    }

    return code;
}

而后就是生成的核心代码了。.net

private static readonly Color[] Colors = { Color.Black, Color.Red, ... };
private static readonly char[] Chars = { '0', .... };
private static readonly int Width = 90;
private static readonly int Height = 35;

public static (string code, byte[] bytes) GenVCode(int num)
{
    var code = GenCode(num);
    var r = new Random();

    using var image = new Image<Rgba32>(Width, Height);
    
    // 字体
    var font = SystemFonts.CreateFont(SystemFonts.Families.First().Name, 25, FontStyle.Bold);

    image.Mutate(ctx =>
    {
        // 白底背景
        ctx.Fill(Color.White);
    
        // 画验证码
        for (int i = 0; i < code.Length; i++)
        {
            ctx.DrawText(code[i].ToString()
                , font
                , Colors[r.Next(Colors.Length)]
                , new PointF(20 * i + 10, r.Next(2, 12)));
        }
    
        // 画干扰线
        for (int i = 0; i < 10; i++)
        {
            var pen = new Pen(Colors[r.Next(Colors.Length)], 1);
            var p1 = new PointF(r.Next(Width), r.Next(Height));
            var p2 = new PointF(r.Next(Width), r.Next(Height));

            ctx.DrawLines(pen, p1, p2);
        }

        // 画噪点
        for (int i = 0; i < 80; i++)
        {
            var pen = new Pen(Colors[r.Next(Colors.Length)], 1);
            var p1 = new PointF(r.Next(Width), r.Next(Height));
            var p2 = new PointF(p1.X + 1f, p1.Y + 1f);

            ctx.DrawLines(pen, p1, p2);
        }
    });

    using var ms = new System.IO.MemoryStream();
    
    // gif 格式
    image.SaveAsGif(ms);
    return (code, ms.ToArray());
}

都是中规中矩的代码,这里须要注意下面几个地方:

  1. 关于字体,这里取的是当前系统的第一个,若是系统一个字体都没有,那确定是抛异常的。正常的.net core的docker镜像是没有的,因此这也是为何咱们在前面要准备字体的缘由。
  2. Fill、DrawText、DrawLines这三个画图的方法你们看看说明就能够了
  3. 对于生成格式,老黄这里用的是Gif,它支持Bmp,Gif,Jpeg,Png还有Base64,你们能够按需选择,这里建议用Gif或Jpeg,由于生成的图片会比较小,2.5kb左右。

最后就是调用了。

[HttpGet]
public IActionResult GetCode()
{
    var (code, bytes) = VCodeHelper.GenVCode(4);

    // code handle logic
    System.Console.WriteLine(code);

    return File(bytes, "image/gif");
}

Dockerfile

简单修改一下Dockerfile,加一句复制字体的命令。

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

# 复制字体到 /usr/share/fonts 目录
COPY ./comic.ttf /usr/share/fonts/comic.ttf

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY . .
RUN dotnet restore "VCodeTest.sln"
WORKDIR /src/VCodeTest
RUN dotnet build "VCodeTest.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "VCodeTest.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "VCodeTest.dll"]

效果

生成镜像,运行效果以下:

文中的示例代码能够在个人github找到:

https://github.com/catcherwong-archive/2020/tree/master/05/VCodeTest

小结

ImageSharp 这个组件用起来仍是挺舒服的,你们能够尝试尝试。

相关文章
相关标签/搜索