2009-10-12 55 views
2

我有一个Web应用程序通过WCF客户端与WCF服务进行通信。在我的代码被调用的时候,身份验证cookie已经发布,并且我依赖于期望这些身份验证cookie的ASMX服务。WCF - > ASMX和Cookies

我需要通过WCF客户端将Web应用程序的Cookie传递给WCF服务以传递给ASMX服务。

任何想法?看起来我最好的选择可能是将allowCookies设置为false,解析出Cookie头,尝试在WCF服务上重新创建它们,然后将它们附加到SOAP请求中。

注意:我看着this article,这看起来很接近但不太适用于这个问题。在链接场景中,ASMX服务正在创建Cookie,必须由同一个WCF客户端持久保存到后续的ASMX服务。

+0

谁在发布身份验证Cookie?您的WCF服务使用Cookie还是传递? – 2009-10-13 03:19:23

+0

Cookie由另一个Web应用程序发布。 WCF服务不使用cookie,但需要将它们传递给ASMX服务。 – 2009-10-13 16:36:07

回答

8

好了,有需要发生两两件事:

  1. 从Web应用程序上下文WCF服务
  2. 该cookie获取来自WCF服务的饼干到ASMX服务

注:因为你没有指定,我要去假设你正在使用WCF服务中的WCF客户交谈的ASMX服务。如果情况不是这样,请让我知道,我会相应地修改这篇文章。

第1步:

我会建议使用IEndpointBehavior写你绑定一个IClientMessageInspector到客户端的端点。在您实现IClientMessageInspector::BeforeSendRequest时,您基本上从当前的HttpContext :: Request :: Cookies集合中读取了该cookie,并将该值附加为消息头。这看起来有点像这样:

public void BeforeSendRequest(ref Message request, IClientChannel channel) 
{ 
    // Get the cookie from ASP.NET 
    string cookieValue = HttpContext.Current.Request.Cookies["MyCookieName"].Value; 

    // Create a header that represents the cookie 
    MessageHeader myCookieNameHeader = MessageHeader.CreateHeader("MyCookieHeaderName", "urn:my-custom-namespace", cookieValue); 

    // Add the header to the message 
    request.Headers.Add(myCookieNameHeader); 
} 

一个配置端点与此消息检查每个逻辑请求通过将自动流向cookie的值作为头WCF服务。现在,由于你的WCF服务实际上并不关心标题本身,它可以基本上忽略它。事实上,如果你只做了这一步,你应该能够立即运行你所有的代码,而不会有任何不同。

第2步:

现在我们需要的cookie从WCF服务去ASMX服务。再次

public void BeforeSendRequest(ref Message request, IClientChannel channel) 
{ 
    // Get the cookie value from the custom header we sent in from step #1 
    string cookieValue = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("MyCookieHeaderName", "urn:my-custom-namespace"); 

    HttpRequestMessageHeaderProeperty httpRequestMessageHeaderProperty; 
    MessageProperties outgoingMessageProperties = OperationContext.Current.OutgoingMessageProperties; 

    // Get the HttpRequestMessageHeaderProperty, if it doesn't already exist we create it now 
    if(!outgoingMessageProperties.TryGetValue(HttpRequestMessageHeaderProperty.Name, out httpRequestMessageHeaderProperty)) 
    { 
     httpRequestmessageHeaderProperty = new HttpRequestMessageHeaderProperty(); 

     outgoingMessageProperties.Add(HttpRequestMessageHeaderProperty.Name, httpRequestmessageHeaderProperty); 
    } 

    // Set the cookie header to our cookie value (note: sample assumes no other cookies set) 
    httpRequestmessageHeaderProperty.Headers[HttpRequestHeader.Cookie] = cookieValue; 
} 

你需要把它绑定的端点使用IEndpointBehavior和每个逻辑您的ASMX服务:再次,所有你需要做的就是实现一个IClientMessageInspector,除了你BeforeSendMessageRequest将是一个有点不同请求你将自动通过cookie传递。

+0

[Drew](http:// stackoverflow。com/users/185859/drew-marsh),如果客户端是JavaScript JSON调用,你将如何修改? – dumbledad 2012-03-03 12:18:25

+0

@dumbledad在上面的场景中有几个客户。 Web应用程序客户端从浏览器调用,网页调用WCF服务,然后调用WCF服务调用ASMX服务。我假设你指的是作为JavaScript调用而不是回发的Web应用程序调用? – 2012-03-05 19:40:16

+0

[@Drew](http://stackoverflow.com/users/185859/drew-marsh)我感觉很奇怪,我可以从WCF中的HTTP cookie中读取WebOperationContext.Current.IncomingRequest.Headers [System.Net。 HttpRequestHeader.Cookie],但写入cookie需要一个消息检查器,但我会尽量将其作为一个问题来形式化并单独发布 – dumbledad 2012-03-20 16:38:58