• .NET 6 实现滑动验证码(四)、扩展类


    为了能够通过配置文件(appsettings.json)或通过代码进行背景图片与模板进行配置、可自定义资源类型、自定义验证规则,本节创建一些扩展类,用来实现这些功能。

    上一节内容:NET 6 实现滑动验证码(三)、接口


    扩展类都放在了Extensions文件夹下

    CaptchaBuilderExtensions.cs

    CaptchaBuilderExtensions.cs实现了自定义提供验证码背景图片资源或模板资源、自定义实现资源类型、替换默认验证规则、禁用默认凹槽模板

    using SlideCaptcha;
    using SlideCaptcha.Interface;
    using SlideCaptcha.Resources.Provider;
    using System.Linq;
    
    namespace Microsoft.Extensions.DependencyInjection
    {
        public static class CaptchaBuilderExtensions
        {
            /// 
            /// 自定义提供验证码背景图片资源或模板资源
            /// 
            /// 自定义实现
            /// 
            /// 
            public static CaptchaBuilder AddResourceProvider<TProvider>(this CaptchaBuilder builder) where TProvider : class, IResourceProvider
            {
                builder.Services.AddSingleton();
                return builder;
            }
            /// 
            /// 自定义实现资源类型
            /// 
            /// 自定义实现
            /// 
            /// 
            public static CaptchaBuilder AddResourceHandler<THandler>(this CaptchaBuilder builder) where THandler : class, IResourceHandler
            {
                builder.Services.AddSingleton();
                return builder;
            }
            /// 
            /// 替换默认的验证规则
            /// 
            /// 自定义验证规则
            /// 
            /// 
            public static CaptchaBuilder ReplaceValidator<TValidator>(this CaptchaBuilder builder) where TValidator : class, IValidator
            {
                builder.Services.Replace();
                return builder;
            }
            /// 
            /// 禁用默认凹槽模板
            /// 
            /// 
            /// 
            public static CaptchaBuilder DisableDefaultTemplates(this CaptchaBuilder builder)
            {
                var serviceDescriptor = builder.Services.FirstOrDefault(e => e.ImplementationType == typeof(EmbeddedResourceProvider));
                if (serviceDescriptor != null)
                {
                    builder.Services.Remove(serviceDescriptor);
                }
    
                return builder;
            }
        }
    }
    
    

    CaptchaServiceCollectionExtensions.cs

    CaptchaServiceCollectionExtensions.cs为自动注入必要的接口

    using Microsoft.Extensions.Configuration;
    using SlideCaptcha;
    using SlideCaptcha.Constant;
    using SlideCaptcha.Generator;
    using SlideCaptcha.Interface;
    using SlideCaptcha.Resources;
    using SlideCaptcha.Resources.Handler;
    using SlideCaptcha.Resources.Provider;
    using SlideCaptcha.Storage;
    using SlideCaptcha.Validator;
    using System;
    
    namespace Microsoft.Extensions.DependencyInjection
    {
        public static class CaptchaServiceCollectionExtensions
        {
            /// 
            /// 依赖注入必要的接口。
            /// 
            /// 
            /// 
            /// 
            /// 
            public static CaptchaBuilder AddSlideCaptcha(this IServiceCollection services, IConfiguration configuration, Action optionsAction = default)
            {
                services.Configure(configuration?.GetSection("SlideCaptcha"));
                //对所需验证的参数进行检查,如果参数不合法就抛一个异常出来
                if (optionsAction != null) services.PostConfigure(optionsAction);
    
                var builder = new CaptchaBuilder(services);
                services.AddSingleton();
                services.AddSingleton();
                services.AddSingleton();
                services.AddSingleton();
                services.AddSingleton();
                services.AddSingleton();
                services.AddSingleton();
                services.AddScoped();
                services.AddScoped();
                services.AddScoped();
                return builder;
            }
        }
    }
    
    

    ServiceCollectionExtensions.cs

    ServiceCollectionExtensions.cs用于实现IServiceCollection的Replace方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    
    namespace Microsoft.Extensions.DependencyInjection
    {
        public static class ServiceCollectionExtensions
        {
            public static IServiceCollection Replace<TService, TImplementation>(this IServiceCollection services)
                   where TImplementation : TService
            {
                return services.Replace(typeof(TImplementation));
            }
    
            public static IServiceCollection Replace<TService>(this IServiceCollection services, Type implementationType)
            {
                return services.Replace(typeof(TService), implementationType);
            }
    
            public static IServiceCollection Replace(this IServiceCollection services, Type serviceType, Type implementationType)
            {
                if (services == null)
                {
                    throw new ArgumentNullException(nameof(services));
                }
    
                if (serviceType == null)
                {
                    throw new ArgumentNullException(nameof(serviceType));
                }
    
                if (implementationType == null)
                {
                    throw new ArgumentNullException(nameof(implementationType));
                }
    
                if (!services.TryGetDescriptors(serviceType, out var descriptors))
                {
                    throw new ArgumentException($"No services found for {serviceType.FullName}.", nameof(serviceType));
                }
    
                foreach (var descriptor in descriptors)
                {
                    var index = services.IndexOf(descriptor);
    
                    services.Insert(index, descriptor.WithImplementationType(implementationType));
    
                    services.Remove(descriptor);
                }
    
                return services;
            }
    
            private static bool TryGetDescriptors(this IServiceCollection services, Type serviceType, out ICollection descriptors)
            {
                return (descriptors = services.Where(service => service.ServiceType == serviceType).ToArray()).Any();
            }
    
            private static ServiceDescriptor WithImplementationType(this ServiceDescriptor descriptor, Type implementationType)
            {
                return new ServiceDescriptor(descriptor.ServiceType, implementationType, descriptor.Lifetime);
            }
        }
    }
    
    

    SlideCaptchaException.cs

    SlideCaptchaException.cs 实现自定义异常信息。

    using System;
    
    namespace SlideCaptcha.Exceptions
    {
        public class SlideCaptchaException : Exception
        {
            public SlideCaptchaException() : base()
            {
            }
    
            public SlideCaptchaException(string message) : base(message)
            {
            }
    
            public SlideCaptchaException(string message, Exception innerException) : base(message, innerException)
            {
            }
        }
    }
    
    

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

    欢迎大家关注我的微信公众号,一起进步,一起成长
  • 相关阅读:
    mac 安装docker
    功能安全Part1-名词定义
    关于UIDocumentInteractionController使用问题
    javaEE初阶——多线程(五)
    苏州园林景区网页设计 学生家乡网页设计作品静态HTML网页模板源码 DW大学生家乡主题网站制作 简单家乡介绍网页设计成品
    Docker DeviceMapper占用空间太大解决方案
    追寻红色记忆、晋国文明之思考毕业季研学营
    更改Kali Linux系统语言以及安装zenmap
    常识——手机改直供电+usb调试
    uboot启动学习笔记 一 初步了解及相关概念
  • 原文地址:https://www.cnblogs.com/gmval/p/16957585.html