你好,我在服务器上有一个web服务。我是连接到这个基于BasicHttpBinding的客户端的web服务。我们添加到基于服务cookie的身份验证中,现在我想向我的客户端添加对cookie的支持。
这是我的MyServiceClient课程:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class MyServiceClient: System.ServiceModel.ClientBase<IMyServiceClient>, IMyServiceClient
{
... ctors ....
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
CheckAuthorizationResponse IMyServiceClient.CheckAuthorization(CheckAuthorizationRequest request)
{
return base.Channel.CheckAuthorization(request);
}
... ...
}我就是这样创建客户机的:
var binding = new BasicHttpBinding("MyServiceSoapBinding");
binding.AllowCookies = true; // trying to experiment with this
var client = new MyServiceClient(binding, new EndpointAddress(endpointAddr),installation.CertThumbprint);
return client;这是我的CheckAuthorization实现:
public bool CheckAuthorization()
{
CheckAuthorizationRequest inValue = new CheckAuthorizationRequest();
CheckAuthorizationResponse retVal = ((IMyServiceClient)(this)).CheckAuthorization(inValue);
return retVal.checkAuthorizationResult;
}我知道曲奇的价值,所以让我们说: string = "blabla";
现在,我需要将这个cookie添加到我的客户端,每当我调用CheckAuthorization时,cookie就会传递。有什么办法吗?我以异常结束“HTTP请求被禁止使用客户端身份验证方案‘Basic’”。
发布于 2014-07-02 15:50:26
所以我解决了这个问题,我补充道:
using (new OperationContextScope(client.InnerChannel))
{
var endPoint = new EndpointAddress(url);
var authCookie = new System.Net.Cookie("Auth", cookie);
var cookies = new CookieContainer();
cookies.Add(new Uri(url), authCookie);
var httpRequest = new System.ServiceModel.Channels.HttpRequestMessageProperty();
OperationContext.Current.OutgoingMessageProperties.Add(System.ServiceModel.Channels.HttpRequestMessageProperty.Name, httpRequest);
httpRequest.Headers.Add(HttpRequestHeader.Cookie, cookies.GetCookieHeader(endPoint.Uri));
var authChecked = client.CheckAuthorization(request);
}此外,我必须补充:
var binding = new BasicHttpBinding("MyServiceSoapBinding");
binding.AllowCookies = false;https://stackoverflow.com/questions/24530161
复制相似问题