WSHttpBindingElement.AllowCookies 屬性

定義

取得或設定值,這個值表示 WCF 用戶端是否會自動儲存並重新傳送單一 Web 服務所傳送的任何 Cookie。

public:
 property bool AllowCookies { bool get(); void set(bool value); };
[System.Configuration.ConfigurationProperty("allowCookies", DefaultValue=false)]
public bool AllowCookies { get; set; }
[<System.Configuration.ConfigurationProperty("allowCookies", DefaultValue=false)>]
member this.AllowCookies : bool with get, set
Public Property AllowCookies As Boolean

屬性值

如果自動 Cookies 處理是必要的則為 true,否則為 false

屬性

備註

當用戶端與一個使用 Cookie 的 Web 服務互動時,將 設定 AllowCookiestrue 會很有用。 如果您要存取多個具有相同 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();  
        }  

適用於