WCF ->ASMX 和 Cookie [英] WCF -> ASMX and Cookies

查看:25
本文介绍了WCF ->ASMX 和 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 WCF 客户端与 WCF 服务通信的 Web 应用程序.在调用我的代码时,已发出身份验证 cookie,并且我依赖于需要这些身份验证 cookie 的 ASMX 服务.

I have a web application that communicates to a WCF service through a WCF client. At the point my code is invoked, authentication cookies have been issued and I have a dependency on an ASMX service that expects those authentication cookies.

我需要将来自 Web 应用程序的 cookie 通过 WCF 客户端传递到 WCF 服务到 ASMX 服务.

I need to pass the cookies from the web application through the WCF client to the WCF service to the ASMX service.

有什么想法吗?看起来我最好的选择可能是将 allowCookies 设置为 false,解析出 cookie 标头,尝试在 WCF 服务上重新创建它们,然后将它们附加到 SOAP 请求.

Any ideas? It looks like my best bet may be setting allowCookies to false, parsing out the cookie headers, attempting to re-create them on the WCF service and then attaching them to the SOAP request.

注意:我查看了这篇文章,这似乎很接近,但不太适用于这个问题.在链接方案中,ASMX 服务正在创建 cookie,这些 cookie 必须由同一个 WCF 客户端持久保存到后续 ASMX 服务.

Note: I looked at this article, which seems close but not quite applicable to this question. In the linked scenario, an ASMX service is creating cookies, which must be persisted to a subsequent ASMX service by the same WCF client.

推荐答案

好的,有两个主要的事情需要发生:

Ok, so there's two main things that need to occur:

  1. 从 Web 应用上下文获取 cookie 到 WCF 服务
  2. 从WCF服务获取cookie到ASMX服务

注意:由于您没有指定,我将假设您在 WCF 服务中使用 WCF 客户端与 ASMX 服务通信.如果不是这种情况,请告诉我,我会相应地修改这篇文章.

NOTE: Because you didn't specify, I'm going to assume that you're using a WCF client within your WCF service to talk to the ASMX service. If this is not the case please let me know and I will revise this post accordingly.

第 1 步:

我建议编写一个 IClientMessageInspector您使用 IEndpointBehavior.在您实施 IClientMessageInspector::BeforeSendRequest 您基本上是从当前 HttpContext::Request::Cookies 集合中读取 cookie 并将该值附加为消息头.这看起来有点像这样:

I would recommend writing an IClientMessageInspector which you bind to your client endpoints using an IEndpointBehavior. In your implementation of IClientMessageInspector::BeforeSendRequest you basically read the cookie out of the current HttpContext::Request::Cookies collection and append the value as a message header. That would look a little something like this:

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 服务实际上并不关心标头本身,它基本上可以忽略它.事实上,如果你只做了这一步,你现在应该可以运行你的所有代码并且不会有任何差异.

One you configure the endpoint with this message inspector every logical request will automatically flow the cookie value through as a header to your WCF service. Now, since your WCF service doesn't actually care about the header itself, it can basically ignore it. In fact, if you only did this step, you should be able to run all your code right now and not experience any difference.

第 2 步:

现在我们需要将 cookie 从 WCF 服务传送到 ASMX 服务.同样,您需要做的就是实现一个 IClientMessageInspector,除了你的 BeforeSendMessageRequest 会有点不同:

Now we need the cookie to go from the WCF service to the ASMX service. Once again all you need to do is implement an IClientMessageInspector, except your BeforeSendMessageRequest is going to be a little different:

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 以及您发出的每个逻辑请求都会自动传递 cookie.

Once again you need to bind this to the endpoint for your ASMX service using an IEndpointBehavior and every logical request you make will automatically pass the cookie through.

这篇关于WCF ->ASMX 和 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆