CaptchaMVC6.CaptchaMiddleware.Invoke C# (CSharp) Method

Invoke() public method

Process an individual request.
public Invoke ( Microsoft.AspNet.Http.HttpContext context ) : System.Threading.Tasks.Task
context Microsoft.AspNet.Http.HttpContext
return System.Threading.Tasks.Task
        public Task Invoke(HttpContext context)
        {
            var request = context.Request;

            var path = new PathString(Path);
            if (request.Path == path)
            {
                var codeName = StringValues.IsNullOrEmpty(request.Query["name"]) ? "default" : request.Query["name"].ToString();
                var length = request.Query["length"];
                var codeLength = StringValues.IsNullOrEmpty(length) ? 4 : int.Parse(length);
                string codeValue = _codeGenerator.Generate(codeLength);
                context.Session.SetString(SessionKeyPrefix_Value + codeName.Trim(), codeValue);
                var buffer = _graphicGenerator.Generate(codeValue);

                context.Response.ContentLength = buffer.Length;
                context.Response.ContentType = "image/jpeg";
                context.Response.StatusCode = 200;
                return context.Response.Body.WriteAsync(buffer, 0, buffer.Length);
            }
            return _next.Invoke(context);
        }