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

时间:2014-01-07 05:55:23

标签: c# .net wcf authentication cookies

Basic security Authentication

中实施WCF Service.

管理从Web服务获取.ASPXCookie。但是,如何将收到的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"];
                }
            }

我尝试打印SharedCookie并成功了。 它看起来像,

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

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

1 个答案:

答案 0 :(得分:8)

如果您已经拥有cookie字符串,则在当前上下文中将Cookie标头添加到WCF请求:

var prop = new HttpRequestMessageProperty();
prop.Headers.Add(HttpRequestHeader.Cookie, sharedCookie);
OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, prop);