.NET 6 实现滑动验证码(五)、验证码设置与缓存

  • .NET 6 实现滑动验证码(五)、验证码设置与缓存已关闭评论
  • 123 次浏览
  • A+
所属分类:.NET技术
摘要

这一节,我们在Constant目录中,定义两个类CaptchaOptions.cs与CaptchaTypeConstant。目的是设置验证码的类型与其他一些属性。然后在Storage目录中,设置验证码的缓存数据。

这一节,我们在Constant目录中,定义两个类CaptchaOptions.cs与CaptchaTypeConstant。目的是设置验证码的类型与其他一些属性。然后在Storage目录中,设置验证码的缓存数据。

上一节内容:.NET 6 实现滑动验证码(四)、扩展类


CaptchaOptions.cs

Constant 文件夹下新建立CaptchaOptions.cs。定义验证码过期时间、缓存key值、容错值等。

using SlideCaptcha.Model; using System.Collections.Generic;  namespace SlideCaptcha.Constant {     public class CaptchaOptions     {         /// <summary>         /// 过期时长         /// </summary>         public int ExpirySeconds { get; set; } = 60;          /// <summary>         /// 存储键前缀         /// </summary>         public string StoreageKeyPrefix { get; set; } = "slide-captcha";          /// <summary>         /// 容错值(校验时用,缺口位置与实际滑动位置匹配容错范围)         /// </summary>         public float Tolerant { get; set; } = 0.02f;          /// <summary>         /// 背景图         /// </summary>         public List<Resource> Backgrounds { get; set; } = new List<Resource>();          /// <summary>         /// 模板图(必须是slider,notch的顺序依次出现)         /// </summary>         public List<TemplatePair> Templates { get; set; } = new List<TemplatePair>();     } }  

CaptchaTypeConstant.cs

Constant 文件夹下新建立CaptchaTypeConstant.cs。定义各种常见验证码类型:

namespace SlideCaptcha.Constant {     public class CaptchaTypeConstant     {         /** 滑块. */         public static string SLIDER = "SLIDER";         /** 旋转. */         public static string ROTATE = "ROTATE";         /** 拼接.*/         public static string CONCAT = "CONCAT";         /** 图片点选.*/         public static string IMAGE_CLICK = "IMAGE_CLICK";         /** 文字图片点选.*/         public static string WORD_IMAGE_CLICK = "WORD_IMAGE_CLICK";      } }  

本次我们只实现滑块验证码。旋转、拼接、图片点选,文字图片点选暂不考虑。不过在源码中,已添加了旋转验证码的实现,目前还在测试中。

DefaultStorage.cs

Storage文件夹建立DefaultStorage.cs,定义缓存的各种方法。

using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Options; using Newtonsoft.Json; using SlideCaptcha.Constant; using SlideCaptcha.Interface; using System; using System.Text;  namespace SlideCaptcha.Storage {     public class DefaultStorage : IStorage     {         private readonly IDistributedCache _cache;         private readonly IOptionsMonitor<CaptchaOptions> _options;          public DefaultStorage(IOptionsMonitor<CaptchaOptions> options, IDistributedCache cache)         {             _options = options;             _cache = cache;         }          private string WrapKey(string key)         {             return $"{this._options.CurrentValue.StoreageKeyPrefix}{key}";         }          public T Get<T>(string key)         {             var bytes = _cache.Get(WrapKey(key));             if (bytes == null) return default(T);             var json = Encoding.UTF8.GetString(bytes, 0, bytes.Length);             return JsonConvert.DeserializeObject<T>(json);         }          public void Remove(string key)         {             _cache.Remove(WrapKey(key));         }          public void Set<T>(string key, T value, DateTimeOffset absoluteExpiration)         {             string json = JsonConvert.SerializeObject(value);             byte[] bytes = Encoding.UTF8.GetBytes(json);              _cache.Set(WrapKey(key), bytes, new DistributedCacheEntryOptions             {                 AbsoluteExpiration = absoluteExpiration             });         }     } }  

缓存使用的是IDistributedCache 接口,方便集成在项目的时候,选择需要的缓存,如MemoryCache或Redis。

下一篇,开始写验证码图片与验证码凹槽的获取。

下载方式:
扫描公众号二维码关注我,回复captcha下载,压缩包包含了验证码类库、服务端API、HTML+JQuery完整代码、vue3组件代码及演示代码!

欢迎大家关注我的微信公众号,一起进步,一起成长
.NET 6 实现滑动验证码(五)、验证码设置与缓存