示例#1
0
        public HomeController(IOptions <SimCaptchaOptions> options, IHttpContextAccessor accessor)
        {
            _options     = options.Value;
            this._client = new SimCaptchaClient(options.Value, new AspNetCoreJsonHelper(), new ConsoleLogHelper());

            this._accessor = accessor;
        }
        public SimCaptchaMiddleware(RequestDelegate next, IOptions<SimCaptchaOptions> optionsAccessor, ICache cache, IHttpContextAccessor accessor, IVCodeImage vCodeImage, IJsonHelper jsonHelper, ILogHelper logHelper)
        {
            _next = next;
            _options = optionsAccessor.Value;

            cache.TimeOut = optionsAccessor.Value.ExpiredSec;

            _service = new SimCaptchaService(
                optionsAccessor.Value,
                cache,
                vCodeImage,
                jsonHelper,
                logHelper
                );
            _accessor = accessor;
            _jsonHelper = jsonHelper;
        }
        public SimCaptchaMiddleware(RequestDelegate next, IOptions <SimCaptchaOptions> optionsAccessor, IMemoryCache memoryCache, IHttpContextAccessor accessor)
        {
            _next    = next;
            _options = optionsAccessor.Value;

            _service = new SimCaptchaService(
                optionsAccessor.Value,
                new LocalCache(memoryCache)
            {
                TimeOut = optionsAccessor.Value.ExpiredSec
            },
                new AspNetCoreVCodeImage(),
                new AspNetCoreJsonHelper()
                );
            _accessor   = accessor;
            _jsonHelper = new AspNetCoreJsonHelper();
        }
示例#4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // 重要: 注册验证码配置, 之后就可以在控制器 通过构造器注入
            services.Configure <SimCaptchaOptions>(Configuration.GetSection(
                                                       SimCaptchaOptions.SimCaptcha));
            SimCaptchaOptions simCaptchaOptions = new SimCaptchaOptions();

            Configuration.GetSection(SimCaptchaOptions.SimCaptcha).Bind(simCaptchaOptions);
            IEnumerable <List <string> > temp = simCaptchaOptions.AppList?.Select(m => m.CorsWhiteList);
            // 所有允许跨域的 Origin
            List <string> allAllowedCorsOrigins = new List <string>();

            foreach (var corsWhiteList in temp)
            {
                foreach (var item in corsWhiteList)
                {
                    allAllowedCorsOrigins.Add(item);
                }
            }

            // 允许 AspNetCoreClient 跨域请求
            services.AddCors(options =>
            {
                options.AddPolicy(name: VCodeAllowSpecificOrigins,
                                  builder =>
                {
                    // SimCaptchaOptions 里配置的白名单都允许
                    builder.WithOrigins(allAllowedCorsOrigins.ToArray())

                    // 解决发送json,复杂请求问题: https://blog.csdn.net/yangyiboshigou/article/details/78738228
                    // 解决方法: Access-Control-Allow-Headers: Content-Type
                    // TODO: 测试,在Asp.Net Core中, Action实体形参只能传json, 不能用application/x-www-form-urlencoded, 否则 HTTP 415, 原因分析: 应该是因为客户端发送数据时用的json格式,没有转换为FormData格式
                    // 参考: https://www.cnblogs.com/jpfss/p/10102132.html
                    .WithHeaders("Content-Type");
                });
            });

            // 用于获取ip地址
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            // 用于 SimCaptcha.AspNetCore.LocalCache 缓存
            services.AddMemoryCache();

            services.AddControllers();
        }
示例#5
0
        public VCodeController(
            IOptions <SimCaptchaOptions> options,
            IHttpContextAccessor accessor,
            IMemoryCache memoryCache)
        {
            _options      = options.Value;
            this._service = new SimCaptchaService(
                new LocalCache(memoryCache)
            {
                TimeOut = options.Value.ExpiredSec
            },
                new AspNetCoreVCodeImage(),
                new AspNetCoreJsonHelper(),
                options.Value);
            this._appChecker = new DefaultAppChecker(options.Value);

            this._accessor = accessor;
        }
示例#6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // 1.重要: 注册验证码配置, 之后就可以在控制器 通过构造器注入
            services.Configure <SimCaptchaOptions>(Configuration.GetSection(
                                                       SimCaptchaOptions.SimCaptcha));

            #region 跨域配置 (若业务后台与验证码服务端同源, 则不需要配置)
            SimCaptchaOptions simCaptchaOptions = new SimCaptchaOptions();
            Configuration.GetSection(SimCaptchaOptions.SimCaptcha).Bind(simCaptchaOptions);
            IEnumerable <List <string> > temp = simCaptchaOptions.AppList?.Select(m => m.CorsWhiteList);
            // 所有允许跨域的 Origin
            List <string> allAllowedCorsOrigins = new List <string>();
            foreach (var corsWhiteList in temp)
            {
                foreach (var item in corsWhiteList)
                {
                    allAllowedCorsOrigins.Add(item);
                }
            }

            // 允许 AspNetCoreClient 跨域请求
            services.AddCors(options =>
            {
                options.AddPolicy(name: VCodeAllowSpecificOrigins,
                                  builder =>
                {
                    // SimCaptchaOptions 里配置的白名单都允许
                    builder.WithOrigins(allAllowedCorsOrigins.ToArray())

                    // 解决发送json,复杂请求问题: https://blog.csdn.net/yangyiboshigou/article/details/78738228
                    // 解决方法: Access-Control-Allow-Headers: Content-Type
                    // 参考: https://www.cnblogs.com/jpfss/p/10102132.html
                    .WithHeaders("Content-Type");
                });
            });
            #endregion

            // 2.添加 SimCaptcha
            services.AddSimCaptcha();

            services.AddControllers();
        }
示例#7
0
        public static IApplicationBuilder UseSimCaptcha(this IApplicationBuilder builder, SimCaptchaOptions options)
        {
            builder.Map("/api/vCode/vCodeImg", app => app.UseMiddleware <VCodeImgMiddleware>(new OptionsWrapper <SimCaptchaOptions>(options)));
            builder.Map("/api/vCode/vCodeCheck", app => app.UseMiddleware <VCodeCheckMiddleware>(new OptionsWrapper <SimCaptchaOptions>(options)));
            builder.Map("/api/vCode/ticketVerify", app => app.UseMiddleware <TicketVerifyMiddleware>(new OptionsWrapper <SimCaptchaOptions>(options)));

            return(builder);
        }
示例#8
0
 public DefaultAppChecker(SimCaptchaOptions options)
 {
     this.Options = options;
 }