Beispiel #1
0
            private void OverwriteRequestedContent(ProcessingContext context)
            {
                StringBuilder pageContentBuilder = new StringBuilder("<!DOCTYPE html>");

                pageContentBuilder.AppendLine("<html>");
                pageContentBuilder.AppendLine("<head>");
                pageContentBuilder.AppendLine("  <title>Hello</title>");
                pageContentBuilder.AppendLine("</head>");
                pageContentBuilder.AppendLine("<body>");
                pageContentBuilder.AppendLine("  <h3>Hello, world!</h3>");
                pageContentBuilder.AppendLine("</body>");
                pageContentBuilder.AppendLine("</html>");
                string pageContent = pageContentBuilder.ToString();

                context.StopProcessing();
                MemoryStream responseStream = new MemoryStream(Encoding.UTF8.GetBytes(pageContent));
                var          responseHeader = new BenderProxy.Headers.HttpResponseHeader(200, "OK", "1.1");

                responseHeader.EntityHeaders.ContentType     = "text/html";
                responseHeader.EntityHeaders.ContentEncoding = "utf-8";
                responseHeader.EntityHeaders.ContentLength   = responseStream.Length;
                new HttpResponseWriter(context.ClientStream).Write(responseHeader, responseStream, responseStream.Length);
            }
        private void ProcessResponse(ProcessingContext context)
        {
            // Only do any processing on the response if the response is 401,
            // or "Unauthorized".
            if (context.ResponseHeader != null && context.ResponseHeader.StatusCode == 401)
            {
                //context.ServerStream.ReadTimeout = 5000;
                //context.ServerStream.WriteTimeout = 5000;
                var reader = new StreamReader(context.ServerStream);
                if (context.ResponseHeader.EntityHeaders.ContentLength != 0)
                {
                    ReadFromStream(reader);
                }
                // We do not want the proxy to do any further processing after
                // handling this message.
                context.StopProcessing();


                if (context.ResponseHeader.WWWAuthenticate.Contains(BasicGenerator.AuthorizationHeaderMarker))
                {
                    // This is the generation of the HTTP Basic authorization header value.
                    var basicAuthHeaderValue = $"user:pass";
                    var encodedHeaderValue   = Convert.ToBase64String(Encoding.ASCII.GetBytes(basicAuthHeaderValue));
                    context.RequestHeader.Authorization = "Basic " + encodedHeaderValue;

                    // Resend the request (with the Authorization header) to the server
                    // using BenderProxy's HttpMessageWriter.
                    var writer = new HttpMessageWriter(context.ServerStream);
                    writer.Write(context.RequestHeader);

                    // Get the authorized response, and forward it on to the browser, using
                    // BenderProxy's HttpHeaderReader and support classes.
                    var    headerReader = new HttpHeaderReader(reader);
                    var    header       = new HttpResponseHeader(headerReader.ReadHttpMessageHeader());
                    var    body         = ReadFromStream(reader);
                    Stream bodyStream   = new MemoryStream(Encoding.UTF8.GetBytes(body));
                    new HttpResponseWriter(context.ClientStream).Write(header, bodyStream, bodyStream.Length);
                }
                else if (context.ResponseHeader.WWWAuthenticate.Contains(NtlmGenerator.AuthorizationHeaderMarker))
                {
                    // Read the WWW-Authenticate header. Because of the way the test
                    // web app is configured, it returns multiple headers, with
                    // different schemes. We need to select the correct one.
                    var authHeader = GetAuthenticationHeader(context.ResponseHeader.WWWAuthenticate,
                                                             NtlmGenerator.AuthorizationHeaderMarker);

                    var type1            = new NtlmNegotiateMessageGenerator();
                    var type1HeaderValue = type1.GenerateAuthorizationHeader();
                    context.RequestHeader.Authorization = type1HeaderValue;
                    var writer = new HttpMessageWriter(context.ServerStream);
                    writer.Write(context.RequestHeader);

                    var headerReader        = new HttpHeaderReader(reader);
                    var challengeHeader     = new HttpResponseHeader(headerReader.ReadHttpMessageHeader());
                    var challengeAuthHeader = challengeHeader.WWWAuthenticate;
                    var challengeBody       = ReadFromStream(reader);

                    if (!string.IsNullOrEmpty(challengeAuthHeader) &&
                        challengeAuthHeader.StartsWith(NtlmGenerator.AuthorizationHeaderMarker))
                    {
                        // If a proper message was received (the "type 2" or "Challenge" message),
                        // parse it, and generate the proper authentication header (the "type 3"
                        // or "Authorization" message).
                        var type2            = new NtlmChallengeMessageGenerator(challengeAuthHeader);
                        var type3            = new NtlmAuthenticateMessageGenerator(null, null, "user", "pass", type2);
                        var type3HeaderValue = type3.GenerateAuthorizationHeader();
                        context.RequestHeader.Authorization = type3HeaderValue;
                        writer.Write(context.RequestHeader);

                        // Get the authorized response from the server, and forward it on to
                        // the browser.
                        var    header     = new HttpResponseHeader(headerReader.ReadHttpMessageHeader());
                        var    body       = ReadFromStream(reader);
                        Stream bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(body));
                        new HttpResponseWriter(context.ClientStream).Write(header, bodyStream, bodyStream.Length);
                        context.ClientStream.Flush();
                    }
                }
            }
        }