如何在 "Set-Cookie" header 的新请求中为 WPF 应用程序中的 SOAP 客户端设置 "Cookie" header
How do I set "Cookie" header in a new request from "Set-Cookie" header response for a SOAP client in a WPF application
我正在将我的 WPF 应用程序与 2 个 WCF 应用程序集成(一个是 "Authentication Application",另一个是需要身份验证的 "real" 应用程序)。 "Authentication Application" returns 3 Set-Cookie
header,我需要将它们添加到 "real" 应用程序的请求 header 中。但是我不确定如何得到那些 headers(只有结果)我能得到:
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
returns true 或 false 是否成功登录,headers 包括 Set-Cookie
。我如何获取那些 headers 并在下一个请求中设置 Cookie
headers 到 PostAsync
?
如果还有问题,请告诉我!
您应该使用 OperationContext,它具有可以发送 cookie 的属性。
要启用 cookie ,您应该在绑定配置中将 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,参考link最后一段代码
https://megakemp.com/2009/02/06/managing-shared-cookies-in-wcf/
我正在将我的 WPF 应用程序与 2 个 WCF 应用程序集成(一个是 "Authentication Application",另一个是需要身份验证的 "real" 应用程序)。 "Authentication Application" returns 3 Set-Cookie
header,我需要将它们添加到 "real" 应用程序的请求 header 中。但是我不确定如何得到那些 headers(只有结果)我能得到:
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
returns true 或 false 是否成功登录,headers 包括 Set-Cookie
。我如何获取那些 headers 并在下一个请求中设置 Cookie
headers 到 PostAsync
?
如果还有问题,请告诉我!
您应该使用 OperationContext,它具有可以发送 cookie 的属性。 要启用 cookie ,您应该在绑定配置中将 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,参考link最后一段代码 https://megakemp.com/2009/02/06/managing-shared-cookies-in-wcf/