3

Been implementing the Basic security Authentication in a WCF Service.

Managed to get the .ASPXCookie from the web service. But, how to pass along the received cookie back to the next request?

var authClient = new MovieDbClient();
using (new OperationContextScope(authClient.InnerChannel))
{
    isValid = authClient.Login("userName", "passWord*");
    if (isValid)
    {
        var response = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
        sharedCookie = response.Headers["Set-Cookie"];
    }
}

I tried to print the SharedCookie and was successful in it. It looks something like,

".ASPXAUTH=E499CA76EAC178A96BE5CA1E314CC90E0A6F9B95AD221EF5AD7D43598E701DC034D40904DBB8ECFBFB3EA21F2597D3C8DAB9B19A0491FD5858E9F0A4B6DC6E6A980FBB4CCADE191855A029CF8236C6890BEE28665C236992632807D1021AA138; expires=Tue, 07-Jan-2014 06:22:22 GMT; path=/; HttpOnly"

The question is how do I pass this cookie information in my next request using wCF Client - authClient ?

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37

1 Answers1

10

To add a Cookie header to the WCF request in the current context given that you have the cookie string already:

var prop = new HttpRequestMessageProperty();
prop.Headers.Add(HttpRequestHeader.Cookie, sharedCookie);
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, prop);
Nicholas W
  • 2,231
  • 1
  • 14
  • 15
  • Note, that the great solution of Nicholas W only works, if the flag "AllowCookies" is NOT set to "true" in the Binding of the app.config ServiceModel !!! > http://www.wiktorzychla.com/2011/11/managing-cookies-in-wcf-client.html – Vanden Nov 08 '15 at 20:45