簡體   English   中英

我在哪里設置服務參考的CookieContainer?

[英]Where do I set the CookieContainer on a Service Reference?

例如,在.NET 2.0項目上向ASMX服務添加WebService引用時,

var objService = new NameSpace.groupservices();

那里存在,

objService.CookieContainer = new System.Net.CookieContainer();

例如,在.NET 4.0項目上將ServiceReference添加到ASMX服務時,

var objService = new NameSpace.groupservicesSoapClient();

objService沒有任何CookieContainer屬性

這里提出一個類似的問題,沒有積極的解決方案

有人可以指導在哪里找到該物業?

打開app.config文件並將allowCookies =“true”添加到綁定中。

像這樣的東西:

<binding allowCookies="true" />

與綁定到HTTP傳輸的ASMX Web服務相比,WCF允許使用各種傳輸協議。 因此,並非所有特定於協議的選項(例如用於HTTP傳輸的Cookie)都可在WCF服務引用中使用。

但是,您可以添加一個消息檢查器來檢查在客戶端和服務器之間發送的消息。 本文介紹了將cookie發送到服務器的方法。

我已經擴展了示例以使用CookieContainer。 此外,以下代碼顯示如何評估服務器發送的Set-Cookie標頭,以將新cookie添加到容器中。 請注意,示例顯示了基本大綱,但可能需要擴展或更多驗證。 但是,在一個簡單的場景中它起作用。

以下代碼段顯示了一個WCF服務的測試方法,該方法托管在IIS上並集成在ASP.NET框架中。 它基本上回應了以字符串形式發送到服務器的cookie,並添加了兩個新的:

public string GetData(int value)
{
    var reply = string.Join(", ", 
                    from x in HttpContext.Current.Request.Cookies.AllKeys 
                    select x + "=" + HttpContext.Current.Request.Cookies[x].Value);
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("Test", "Test123"));
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("Test2", "Test1234"));
    return reply;
}

以下測試程序為cookie創建CookieContainer,添加演示cookie並為服務的端點注冊新行為:

class Program
{
    static void Main(string[] args)
    {
        var cookieCont = new CookieContainer();
        using(var svc = new TestServiceReference.TestServiceClient())
        {
            cookieCont.Add(svc.Endpoint.Address.Uri, new Cookie("TestClientCookie", "Cookie Value 123"));
            var behave = new CookieBehavior(cookieCont);
            svc.Endpoint.EndpointBehaviors.Add(behave);
            var data = svc.GetData(123);
            Console.WriteLine(data);
            Console.WriteLine("---");
            foreach (Cookie x in cookieCont.GetCookies(svc.Endpoint.Address.Uri))
                Console.WriteLine(x.Name + "=" + x.Value);
        }
        Console.ReadLine();
    }
}

該行為用於添加自定義消息檢查器並移交CookieContainer:

public class CookieBehavior : IEndpointBehavior
{
    private CookieContainer cookieCont;

    public CookieBehavior(CookieContainer cookieCont)
    {
        this.cookieCont = cookieCont;
    }

    public void AddBindingParameters(ServiceEndpoint serviceEndpoint,
        System.ServiceModel.Channels
        .BindingParameterCollection bindingParameters) { }

    public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint,
        System.ServiceModel.Dispatcher.ClientRuntime behavior)
    {
        behavior.MessageInspectors.Add(new CookieMessageInspector(cookieCont));
    }

    public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint,
        System.ServiceModel.Dispatcher
        .EndpointDispatcher endpointDispatcher) { }

    public void Validate(ServiceEndpoint serviceEndpoint) { }
}

消息檢查器在BeforeSendRequest方法BeforeSendRequest請求發送到服務器時添加cookie,並檢索應在AfterReceiveReply方法中更新的AfterReceiveReply 需要注意的是correlationState由歸國BeforeSendRequest用於檢索在開放的AfterReceiveReply

public class CookieMessageInspector : IClientMessageInspector
{
    private CookieContainer cookieCont;

    public CookieMessageInspector(CookieContainer cookieCont)
    {
        this.cookieCont = cookieCont;
    }

    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply,
        object correlationState) 
    {
        object obj;
        if (reply.Properties.TryGetValue(HttpResponseMessageProperty.Name, out obj))
        {
            HttpResponseMessageProperty httpResponseMsg = obj as HttpResponseMessageProperty;
            if (!string.IsNullOrEmpty(httpResponseMsg.Headers["Set-Cookie"]))
            {
                cookieCont.SetCookies((Uri)correlationState, httpResponseMsg.Headers["Set-Cookie"]);
            }
        }
    }

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request,
        System.ServiceModel.IClientChannel channel)
    {
        object obj;
        if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
        {
            HttpRequestMessageProperty httpRequestMsg = obj as HttpRequestMessageProperty;
            SetRequestCookies(channel, httpRequestMsg);
        }
        else
        {
            var httpRequestMsg = new HttpRequestMessageProperty();
            SetRequestCookies(channel, httpRequestMsg);
            request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMsg);
        }

        return channel.RemoteAddress.Uri;
    }

    private void SetRequestCookies(System.ServiceModel.IClientChannel channel, HttpRequestMessageProperty httpRequestMessage)
    {
        httpRequestMessage.Headers["Cookie"] = cookieCont.GetCookieHeader(channel.RemoteAddress.Uri);
    }
}

在這里找到解決方案:

http://msdn.microsoft.com/en-us/library/bb628649.aspx

事實證明我需要一個Web引用而不是服務引用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM