好了,有需要发生两两件事:
- 从Web应用程序上下文WCF服务
- 该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传递。
谁在发布身份验证Cookie?您的WCF服务使用Cookie还是传递? – 2009-10-13 03:19:23
Cookie由另一个Web应用程序发布。 WCF服务不使用cookie,但需要将它们传递给ASMX服务。 – 2009-10-13 16:36:07