WSHttpBinding.AllowCookies 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个无论 WCF 客户端是否将自动存储并通过单个网页服务重新发送任何 cookie 的值。
public:
property bool AllowCookies { bool get(); void set(bool value); };
public bool AllowCookies { get; set; }
member this.AllowCookies : bool with get, set
Public Property AllowCookies As Boolean
属性值
true
如果需要自动 cookie 操作;否则,false
。
注解
true
当客户端与使用 Cookie 的一个 Web 服务交互时,设置为AllowCookies有用。 如果要使用同一 Cookie 访问多个服务,请设置为AllowCookiesfalse
,并且必须手动将 Cookie 标头添加到每个传出消息。 以下代码演示了如何执行此操作:
MyWebServiceClient client = new MyWebServiceClient();
using (new OperationContextScope(client.InnerChannel))
{
client.DoSomething();
// Extract the cookie embedded in the received web service response
// and stores it locally
HttpResponseMessageProperty response = (HttpResponseMessageProperty)
OperationContext.Current.IncomingMessageProperties[
HttpResponseMessageProperty.Name];
sharedCookie = response.Headers["Set-Cookie"];
}
MyOtherWebServiceClient otherClient = new MyOtherWebServiceClient();
using (new OperationContextScope(otherClient.InnerChannel))
{
// Embeds the extracted cookie in the next web service request
// Note that we manually have to create the request object since
// since it doesn't exist yet at this stage
HttpRequestMessageProperty request = new HttpRequestMessageProperty();
request.Headers["Cookie"] = sharedCookie;
OperationContext.Current.OutgoingMessageProperties[
HttpRequestMessageProperty.Name] = request;
otherClient.DoSomethingElse();
}