c# - 如何在 WPF 应用程序中的 SOAP 客户端的 "Cookie" header 响应的新请求中设置 "Set-Cookie" header

标签 c# wpf wcf soap wsdl

我正在将我的 WPF 应用程序与 2 个 WCF 应用程序集成(一个是“身份验证应用程序”,另一个是需要身份验证的“真实”应用程序)。 “身份验证应用程序”返回 3 个 Set-Cookie header ,我需要将它们添加到“真实”应用程序的请求 header 中。但我不确定如何获取这些 header (仅结果):

AuthenticationApplicationService.SoapClient authenticationSoapClient = new AuthenticationApplicationService.SoapClient("AuthenticationApplicationServiceSoap");
bool loggedInSuccess = await authenticationSoapClient.PerformLoginAsync();
// how do I get the cookie headers from this call and set them on the next?

RealService.SoapClient realSoapClient = new RealService.SoapClient("RealServiceSoap");
realSoapClient.PostAsync("hello");

第一次调用 PerformLoginAsync 会返回 true 或 false 表示登录成功,并且 header 包含 Set-Cookie。如何获取这些 header 并在下一个请求 PostAsync 时设置 Cookie header ?

如果还有其他问题,请告诉我!

最佳答案

您应该使用OperationContext,它具有可以发送cookie的属性。 要启用cookie,您应该在绑定(bind)配置中将allowcookie属性设置为true。

 <bindings>
  <basicHttpBinding>
    <binding name="AuthSoap" allowCookies="true" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:63599/Auth" binding="basicHttpBinding"
    bindingConfiguration="AuthSoap" contract="Auth.AuthSoap" name="AuthSoap" />
</client>

然后您可以按如下方式调用登录方法。

 Auth.AuthSoapClient authSoapClient = new Auth.AuthSoapClient();
        using (new OperationContextScope(authSoapClient.InnerChannel))
        {
// please put the call of method in OperationContextScope
            authSoapClient.Login("admin", "admin");

          // the following code get the set-cookie header passed by server

            HttpResponseMessageProperty response = (HttpResponseMessageProperty)
            OperationContext.Current.IncomingMessageProperties[
                HttpResponseMessageProperty.Name];
            string header  = response.Headers["Set-Cookie"];
          // then you could save it some place  
        }

获得 cookie 后,您应该在每次调用方法时将其设置在 header 中。

 Auth.AuthSoapClient authSoapClient = new Auth.AuthSoapClient();
        using (new OperationContextScope(authSoapClient.InnerChannel))
        {
             //below code sends the cookie back to server

            HttpRequestMessageProperty request = new HttpRequestMessageProperty();
            request.Headers["Cookie"] =  please get the cookie you stored when you   successfully logged in               
 OperationContext.Current.OutgoingMessageProperties[
                HttpRequestMessageProperty.Name] = request;
// please put the call of method in OperationContextScope
            string msg = authSoapClient.GetMsg();
        }

如果你觉得登录成功后每次都发送cookie很麻烦,可以考虑使用MessageInspector,引用链接最后一段代码 https://megakemp.com/2009/02/06/managing-shared-cookies-in-wcf/

关于c# - 如何在 WPF 应用程序中的 SOAP 客户端的 "Cookie" header 响应的新请求中设置 "Set-Cookie" header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54244911/

相关文章:

c# - 如何使用NLog写入多个 "data"文件

c# - 什么可能导致性能提高? GC 时间、池化

wpf - 如何自定义通用异常消息 "Value ' ' 无法转换“

wcf - 什么是 WCF 以及它如何工作?

c# - 使用 Entity Framework 将字符串转换为 DateTime

c# - .NET SoundPlayer问题

c# - WPF : Re-usable template for image buttons?

c# - 如何将多边形绑定(bind)到 WPF 中的现有 PointCollection?

wcf - 使用 webMessageEncoding 的 webHttpBinding : how to configure?

.net - 制作 Silverlight 和常规 .NET REST 客户端的最便携方式是什么