使用WCF客户端+ cookie进行Web服务调用

now*_*ed. 3 .net c# authentication cookies wcf

一直贯彻着Basic security AuthenticationWCF Service.

管理.ASPXCookie从Web服务获取.但是,如何将收到的cookie传递回下一个请求?

            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"];
                }
            }
Run Code Online (Sandbox Code Playgroud)

我试图打印SharedCookie并成功.它看起来像,

".ASPXAUTH=E499CA76EAC178A96BE5CA1E314CC90E0A6F9B95AD221EF5AD7D43598E701DC034D40904DBB8ECFBFB3EA21F2597D3C8DAB9B19A0491FD5858E9F0A4B6DC6E6A980FBB4CCADE191855A029CF8236C6890BEE28665C236992632807D1021AA138; expires=Tue, 07-Jan-2014 06:22:22 GMT; path=/; HttpOnly"
Run Code Online (Sandbox Code Playgroud)

问题是how do I pass this cookie information in my next request using wCF Client - authClient

Nic*_*s W 8

要在您拥有cookie字符串的情况下在当前上下文中将Cookie标头添加到WCF请求:

var prop = new HttpRequestMessageProperty();
prop.Headers.Add(HttpRequestHeader.Cookie, sharedCookie);
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, prop);
Run Code Online (Sandbox Code Playgroud)