public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            try
            {
                var result        = new List <CodePoint>();
                var valueProvider = CompositeValueProvider.CreateAsync(((ControllerBase)context.Controller).ControllerContext).Result;
                var code          = valueProvider.GetValue(Field).ToString();
                if (string.IsNullOrEmpty(code))
                {
                    context.ModelState.TryAddModelError(Field, "验证码为空");
                }
                else
                {
                    var provider  = VerificationCodeFactory.GetProvider();
                    var pointList = provider.Decode(code);
                    var checkCode = provider.Decode(context.HttpContext.Session.GetString(VerificationCodeFactory.Option.SessionKey) ?? String.Empty);
                    if (checkCode?.Compare(pointList) != true)
                    {
                        context.ModelState.TryAddModelError(Field, "验证失败");
                    }
                }
            }
            catch
            {
            }

            return(next());
        }
        private Task CheckCode(HttpContext context)
        {
            var    code   = context.Request.Form["code"].ToString();
            string msg    = "验证失败";
            bool   status = false;

            try
            {
                var provider  = VerificationCodeFactory.GetProvider();
                var pointList = provider.Decode(code);
                var checkCode = provider.Decode(context.Session.GetString(VerificationCodeFactory.Option.SessionKey) ?? String.Empty);
                if (pointList != null && checkCode != null)
                {
                    bool checkResult = pointList.Compare(checkCode);
                    if (checkResult)
                    {
                        status = true;
                        msg    = "验证通过";
                    }
                }
            }
            catch
            {
            }
            var content = JsonConvert.SerializeObject(new { Message = msg, Status = status });

            context.Response.ContentType = "application/json";
            return(context.Response.WriteAsync(content));
        }
        private Task GetVerificationCodeImage(HttpContext context)
        {
            var    provider = VerificationCodeFactory.GetProvider();
            var    model    = provider.CreateImage();
            string msg      = string.Format(VerificationCodeFactory.Option.MessageTipFormat, string.Join(" ", model.GetWord()));
            var    content  = JsonConvert.SerializeObject(new { Result = model.ImageBase64String, Message = msg, Count = model.GetWord().Count });

            context.Session.SetString(VerificationCodeFactory.Option.SessionKey, provider.Encode(model));
            context.Response.ContentType = "application/json";
            return(context.Response.WriteAsync(content));
        }
Example #4
0
        public static IApplicationBuilder UseVerificationCode(this IApplicationBuilder builder, Action <VerificationCodeOption> optionSet)
        {
            if (optionSet == null)
            {
                throw new Exception("VerificationCodeOption 不能为空");
            }
            var option = new VerificationCodeOption();

            optionSet(option);
            VerificationCodeFactory.InitOption(option);
            return(builder.UseMiddleware <VerificationCodeMiddleware>());
        }