是否可以在 WCF 中使用 WS2007FederationHttpBinding 支持 HTTP Cookie?

Is it possible to support HTTP Cookies with WS2007FederationHttpBinding in WCF?

我们需要启用 WCF 客户端,该客户端使用 WS2007FederationHttpBinding over AWS 的 Application Load Balancer 并启用粘性。

Client --> ALB --> Server1, Server2

ALB 每次都会附加一个 ALB cookie,以便客户端在会话生命周期内始终可以与同一台服务器通信。客户端在每个会话中执行多次往返,每次它都能收到一个 ALB cookie,但在发出后续请求时无法将它们发回,就好像它根本不关心 HTTP Cookie 一样。有没有办法使用 WS2007FederationHttpBinding 在 WCF 客户端上启用 HTTP Cookie?

我们可以通过控制http在WCF客户端中使用cookieheader:

using ( new OperationContextScope( client.InnerChannel ) )
{
    HttpRequestMessageProperty request = new HttpRequestMessageProperty();
    request.Headers["Cookie"] = "value";
 
    OperationContext.Current.OutgoingMessageProperties[
        HttpRequestMessageProperty.Name] = request;
 
    client.Test();
}

我们直接与通过 Web 服务交换的 HTTP 消息进行交互,在适当的 header 中以字符串形式读取和写入 cookie。

在必须以相同方式管理从客户端应用程序调用的所有 Web 服务的 cookie 的情况下,最好的选择是通过在 WCF 中应用一个非常有用的功能(消息检查器)来选择集中式解决方案。

我们可以在客户端实现IClientMessageInspector接口来处理cookies,关于WCF客户端如何使用cookies,可以参考这个link:

https://megakemp.com/2009/02/06/managing-shared-cookies-in-wcf/