我在哪里设置服务参考的CookieContainer?

例如,在.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属性

这里提出了一个类似的问题没有积极的解决方案。

有人可以指导在哪里找到该物业?

与绑定到HTTP传输的ASMX Web服务相比,WCF允许使用各种传输协议。 因此,并非所有特定于协议的选项(例如用于HTTP传输的Cookie)都可在WCF服务引用中使用。

但是,您可以添加一个消息检查器来检查在客户端和服务器之间发送的消息。 本文介绍了将cookie发送到服务器的方法。

我已经扩展了示例以使用CookieContainer。 此外,以下代码显示如何评估服务器发送的Set-Cookie标头,以将新cookie添加到容器中。 请注意,示例显示了基本大纲,但可能需要扩展或更多validation。 但是,在一个简单的场景中它起作用。

以下代码段显示了一个WCF服务的测试方法,该方法托管在IIS上并集成在ASP.NET框架中。 它基本上回应了以字符串forms发送到服务器的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 。 请注意, BeforeSendRequest返回的BeforeSendRequest用于检索BeforeSendRequest中的Uri:

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

打开app.config文件并将allowCookies =“true”添加到绑定中。

像这样的东西:

  

在这里找到解决方案:

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

事实certificate我需要一个Web引用而不是服务引用