How do I set "allowCookies=true"

Last post 10-19-2009 7:52 AM by prgscd. 4 replies.

Sort Posts:

  • How do I set "allowCookies=true"

    10-15-2009, 2:40 PM
    • Member
      1 point Member
    • prgscd
    • Member since 10-15-2009, 2:35 PM
    • Posts 3

     How do I set "allowCookies=true" in code with the WCF REST Starter kit, without using the declarative model (setting it in the app.config) file?

  • Re: How do I set "allowCookies=true"

    10-16-2009, 1:20 PM
    • Member
      90 point Member
    • randallt
    • Member since 02-11-2009, 4:53 PM
    • Posts 30

    Are you trying to set allowCookies for the HttpClient?  Or for a WCF ChannelFactory proxy?  On the WCF webHttpBinding, "allowCookies" is used to indicate whether the client accepts cookies and propagates them on future requests.  When you programmatically create the ChannelFactory proxy, use the constructor that takes both a binding (webHttpBinding) and Uri, and  set "allowCookies" on the binding:

    Uri baseAddress = new Uri("http://localhost:8000");
    WebServiceHost host = new WebServiceHost(typeof(Service), baseAddress);
    try
    {
        host.Open();
    
        WebHttpBinding binding = new WebHttpBinding() { AllowCookies = true };
        WebChannelFactory<IService> cf = new WebChannelFactory<IService>(binding, new Uri("http://localhost:8000"));
        IService channel = cf.CreateChannel();
        string s;
    
        Console.WriteLine("Calling EchoWithGet via HTTP GET: ");
        s = channel.EchoWithGet("Hello, world");
        Console.WriteLine("   Output: {0}", s);
    }
    catch (CommunicationException ex)
    {
        Console.WriteLine("An exception occurred: " + ex.Message);
    }
    


     

    Thanks

  • Re: How do I set "allowCookies=true"

    10-16-2009, 2:27 PM
    • Member
      1 point Member
    • prgscd
    • Member since 10-15-2009, 2:35 PM
    • Posts 3

    Thanks for the reply.  I should have provided more detail. I am trying to set "allowCookies" for the HttpClient.  Additionally, I am trying to consume a REST service which was not written in .Net.  I am also using the WCF REST Starter Kit, as I am not that familiar with WCF. 

    Here is the code I'm trying to execute.  This function works and is suppose to return a cookie to maintain state information.  It should be passed on a subsequent "Get" call.  However, the service is returning an error stating that there is no login information on a subsequent "Get" call.  I used fiddler and cannot see the cookie being passed in the Request Header for the "Get". 

        Public Sub Login()
            Static serviceOmnicastLoginUri As String = "http://localhost:3000/xml "
            Dim client As New HttpClient
            Dim _omnicastLoginType As New OmnicastLoginType.login

            _omnicastLoginType.username = "userid"
            _omnicastLoginType.password = "passowrd"

           System.Net.ServicePointManager.Expect100Continue = False

            Using response As HttpResponseMessage = client.Post(serviceOmnicastLoginUri, httpContentExtensions.CreateXmlSerializable(_omnicastLoginType))

                'Throws an exception if not OK
                response.EnsureStatusIs(HttpStatusCode.OK)

                Dim omnicastAboutType = response.Content.ReadAsXmlSerializable(Of OmnicastAboutType.result)()

            End Using

     

    I also tried updating the app.config file to provide the parameters.  However, I dont' know what the service contract is suppose to be for a REST Service.  I was using "xml" as this part of the uri I'm calling. 

    <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="Omnicast" allowCookies="true">
            </binding>
          </webHttpBinding>
        </bindings>
        <client>
          <endpoint address="http//localhost:3000"
                    binding="Omnicast"
                    contract="xml">
          </endpoint>
        </client>
      </system.serviceModel>

  • Re: How do I set "allowCookies=true"

    10-16-2009, 4:09 PM
    Answer
    • Member
      90 point Member
    • randallt
    • Member since 02-11-2009, 4:53 PM
    • Posts 30

    Ok.  If you are using the HttpClient (which I recommend), then setting "allowCookies" on the webHttpBinding won't work for you.  HttpClient is orthogonal to WCF and nothing you set in system.ServiceModel in your webconfig will impact HttpClient's behavior.

    For the HttpClient, you should just be able to instantiate a new CookieContainer:

    HttpClient client = new HttpClient();
    client.TransportSettings.Cookies = new System.Net.CookieContainer();

    This cookie container should see incoming cookies and return them on future requests.

    Please let me know if that doesn't work for you.

    Thanks

  • Re: How do I set "allowCookies=true"

    10-19-2009, 7:52 AM
    • Member
      1 point Member
    • prgscd
    • Member since 10-15-2009, 2:35 PM
    • Posts 3

    That worked!!!  Thank you very much!! 

Page 1 of 1 (5 items)