Ejemplo n.º 1
0
        public HttpChannelFactory(HttpTransportBindingElement source, BindingContext ctx)
            : base(source, ctx)
        {
            ClientCredentials = ctx.BindingParameters.Find <ClientCredentials> ();
            foreach (BindingElement be in ctx.RemainingBindingElements)
            {
                MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
                if (mbe != null)
                {
                    encoder = CreateEncoder <TChannel> (mbe);
                    break;
                }
#if NET_2_1
                var cbe = be as HttpCookieContainerBindingElement;
                if (cbe != null)
                {
                    cookie_manager = cbe.GetProperty <IHttpCookieContainerManager> (ctx);
                }
#endif
            }
            if (encoder == null)
            {
                encoder = new TextMessageEncoder(MessageVersion.Default, Encoding.UTF8);
            }
        }
Ejemplo n.º 2
0
    public static void ChannelFactory_AllowCookies(bool allowCookies)
    {
        ChannelFactory <IWcfService> factory = null;

        try
        {
            factory = new ChannelFactory <IWcfService>(
                new BasicHttpBinding()
            {
                AllowCookies = allowCookies
            },
                new EndpointAddress(FakeAddress.HttpAddress));

            IWcfService serviceProxy = factory.CreateChannel();

            IHttpCookieContainerManager cookieManager = ((IChannel)serviceProxy).GetProperty <IHttpCookieContainerManager>();
            Assert.True(allowCookies == (cookieManager != null),
                        string.Format("AllowCookies was '{0}', 'cookieManager != null' was expected to be '{0}', but it was '{1}'.", allowCookies, cookieManager != null));

            if (allowCookies)
            {
                Assert.True(allowCookies == (cookieManager.CookieContainer != null),
                            string.Format("AllowCookies was '{0}', 'cookieManager.CookieContainer != null' was expected to be '{0}', but it was '{1}'.", allowCookies, cookieManager != null));
            }
        }
        finally
        {
            if (factory != null)
            {
                factory.Close();
            }
        }
    }
Ejemplo n.º 3
0
        private CookieContainer GetCookieContainer()
        {
            IHttpCookieContainerManager property = ((IChannel)this._vimService).GetProperty <IHttpCookieContainerManager>();

            if (property == null)
            {
                throw new InvalidOperationException("Client does not support cookies.");
            }
            return(property.CookieContainer);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a service proxy for a given service contract.
        /// </summary>
        public static T CreateServiceClient <T>(Uri serviceUrl)
        {
            // Determine whether to use transport layer security (HTTP over SSL).
            // Depending on whether HTTPS is used, create a different BindingElement for
            // the transport layer of the Binding.
            bool           useTransportSecurity = serviceUrl.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase);
            BindingElement transportBinding     = useTransportSecurity ?
                                                  new HttpsTransportBindingElement() :
                                                  new HttpTransportBindingElement();

            // Since the ClientHttp stack is used, it is necessary to define cookies manually.
            // We read the cookies from the browser window and attach them to the WCF binding
            // via an additional BindingElement that lets us inject cookies.
            BindingElement cookieBinding = new HttpCookieContainerBindingElement();

            // Create a custom binding that uses the specified elements for transport and
            // accepting cookies. The order of the bindings is relevant; the transport binding
            // needs to come last it's the lowest in the stack.
            Binding binding = new CustomBinding(cookieBinding, transportBinding);

            // Set the address for the service endpoint.
            EndpointAddress address = new EndpointAddress(serviceUrl);

            // Let the ChannelFactory create the service client.
            ChannelFactory <T> factory = new ChannelFactory <T>(binding, address);
            T channel = factory.CreateChannel();

            // Because we've injected the HttpCookieContainerBindingElement, the channel supports cookies:
            // it exposes a CookieContainerManager.
            IHttpCookieContainerManager cookieManager =
                ((IChannel)channel).GetProperty <IHttpCookieContainerManager>();

            // The CookieContainerManager can, but does not have to, provide a Cookie Container;
            // in the case it has not yet been created instantiate one now.
            if (cookieManager.CookieContainer == null)
            {
                cookieManager.CookieContainer = new CookieContainer();
            }

            // Walk through the cookies of the browser window the application lives in and
            // add the cookies to the channel. The URL for the cookie is set to the application root.
            // We get to that URL by quering the application URL - that's the URL of the XAP -
            // and go one level up.
            Uri applicationUri = new Uri(Application.Current.Host.Source, "../");

            foreach (var cookieContent in Cookies)
            {
                Cookie cookie = new Cookie(cookieContent.Key, cookieContent.Value);
                cookieManager.CookieContainer.Add(applicationUri, cookie);
            }
            return(channel);
        }
Ejemplo n.º 5
0
    public static void DefaultSettings_SetCookieOnServerSide()
    {
        // *** SETUP *** \\
        Uri uri     = new Uri(Endpoints.HttpBaseAddress_Basic);
        var factory = new ChannelFactory <IWcfAspNetCompatibleService>(
            new BasicHttpBinding()
        {
            AllowCookies = true
        },
            new EndpointAddress(uri));

        IWcfAspNetCompatibleService serviceProxy = factory.CreateChannel();

        IHttpCookieContainerManager cookieManager = ((IChannel)serviceProxy).GetProperty <IHttpCookieContainerManager>();

        Assert.True(cookieManager != null, "cookieManager was null.");
        Assert.True(cookieManager.CookieContainer != null, "cookieManager.CookieContainer was null.");

        try
        {
            string cookieName = "cookie_time";


            // *** EXECUTE *** \\
            // EchoTimeAndSetCookie returns the current time and also sets the cookie named 'cookieName' to be the same time returned.
            string timeReturned = serviceProxy.EchoTimeAndSetCookie(cookieName);

            CookieCollection cookies = cookieManager.CookieContainer.GetCookies(uri);
            Assert.True(cookies != null, "cookies was null.");

            Cookie cookie = cookies[cookieName];
            Assert.True(cookie != null, "cookie was null.");

            string timeSetInCookie = cookie.Value;

            // *** VALIDATE *** \\
            Assert.True(timeReturned != null, "timeReturned != null");
            Assert.True(timeSetInCookie != null, "timeSetInCookie != null");
            Assert.True(timeReturned == timeSetInCookie,
                        string.Format("Expected cookie named '{0}' to be set to '{1}', but the actual value was '{2}'", cookieName, timeReturned, timeSetInCookie));

            // *** CLEANUP *** \\
            factory.Close();
            ((ICommunicationObject)serviceProxy).Close();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
        }
    }
Ejemplo n.º 6
0
    public static void DefaultSettings_Echo_Cookie()
    {
        // *** SETUP *** \\
        var factory = new ChannelFactory <IWcfAspNetCompatibleService>(
            new BasicHttpBinding()
        {
            AllowCookies = true
        },
            new EndpointAddress(Endpoints.HttpBaseAddress_Basic));

        IWcfAspNetCompatibleService serviceProxy = factory.CreateChannel();

        IHttpCookieContainerManager cookieManager = ((IChannel)serviceProxy).GetProperty <IHttpCookieContainerManager>();

        Assert.True(cookieManager != null, "cookieManager was null.");
        Assert.True(cookieManager.CookieContainer != null, "cookieManager.CookieContainer was null.");
        string cookieName    = "cookieName";
        string cookieValue   = "cookieValue";
        string cookieSentOut = string.Format("{0}={1}", cookieName, cookieValue);

        cookieManager.CookieContainer.Add(new Uri(Endpoints.HttpBaseAddress_Basic), new System.Net.Cookie(cookieName, cookieValue));

        try
        {
            // *** EXECUTE *** \\
            string cookieSentBack = serviceProxy.EchoCookie();

            // *** VALIDATE *** \\
            Assert.True(cookieSentOut == cookieSentBack,
                        string.Format("The expected cookie sent back from the server was '{0}', but the actual cookie was '{1}'", cookieSentOut, cookieSentBack));

            // *** CLEANUP *** \\
            factory.Close();
            ((ICommunicationObject)serviceProxy).Close();
        }
        finally
        {
            // *** ENSURE CLEANUP *** \\
            ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
        }
    }