Esempio n. 1
0
        private static WebRequestHandler GetHandler(string certificateName)
        {
            var handler = new WebRequestHandler();

            // uncomment when need to disable certificate validation
            //handler.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

            if (certificateName != null)
            {
                var certificate = Certificates.ByName(certificateName);
                if (certificate == null)
                {
                    throw new HttpRequestException("Certificate [{0}] not found".ExpandWith(certificateName));
                }
                handler.ClientCertificates.Add(certificate);
            }
            return(handler);
        }
        public static async Task <HttpResponseMessage> ExecuteAsync <TResult>(
            this HttpRequestMessage request,
            Closure <Task <TResult> > closure,
            string certificateName = null)
        {
            var sw = new Stopwatch();

            sw.Start();
            try
            {
#if NO_CERT_CHECK
#else
                if (certificateName != null)
                {
                    var remoteCert = request.GetClientCertificate();
                    if (remoteCert == null)
                    {
                        throw new Exception("No certificate found in the request");
                    }
                    var localCert = Certificates.ByName(certificateName, StoreLocation.CurrentUser);
                    if (localCert == null)
                    {
                        throw new Exception("No certificate found in the store");
                    }
                    if (remoteCert.Thumbprint != localCert.Thumbprint)
                    {
                        throw new Exception("Certificate thumbprints do not match");
                    }
                }
#endif
                var result = request.CreateResponse(HttpStatusCode.OK, await closure());
                result.SetMediaType();
                sw.Stop();
                Log.Information(BuildLogMessage(request, sw));
                return(result);
            }
            catch (HttpException e)
            {
                // Enable callers to define their own Http status code (e.g. 404 BadRequest)
                sw.Stop();
                Log.Error(BuildLogMessage(request, sw, e));

                var httpStatus = HttpStatusCode.InternalServerError;
                if (Enum.IsDefined(typeof(HttpStatusCode), e.GetHttpCode()))
                {
                    httpStatus = (HttpStatusCode)e.GetHttpCode();
                }

                var result = request.CreateResponse(httpStatus, e.Message);
                result.Content.Headers.ContentType.MediaType = "text/html";
                return(result);
            }
            catch (Exception e)
            {
                sw.Stop();
                Log.Error(BuildLogMessage(request, sw, e));
                var result = request.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
                result.Content.Headers.ContentType.MediaType = "text/html";
                return(result);
            }
        }