WSHttpBinding.AllowCookies 屬性

定義

取得或設定值,這個值表示 WCF 用戶端是否會自動儲存並重新傳送單一 Web 服務所傳送的任何 Cookie。Gets or sets a value that indicates whether the WCF client will automatically store and resend any cookies sent by a single web service.

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

屬性值

Boolean

如果自動 Cookies 處理是必要的則為 true,否則為 falsetrue if automatic cookies processing is required; otherwise, false.

備註

AllowCookies true 當用戶端與使用 cookie 的一個 web 服務互動時,將設定為相當有用。Setting AllowCookies to true is useful when a client is interacting with one web service that uses cookies. 如果您要使用相同的 cookie 來存取多個服務,請將設定 AllowCookiesfalse ,而您必須手動將 cookie 標頭新增至每個外寄訊息。If you are accessing multiple services with the same cookie, set AllowCookies to false and you will have to manually add the cookie header to each outgoing message. 下列程式碼示範如何執行這項作業:The following code shows how to do this:

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();  
        }  

適用於