首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向BasicHttpBinding添加cookie

向BasicHttpBinding添加cookie
EN

Stack Overflow用户
提问于 2014-07-02 11:39:06
回答 1查看 2.7K关注 0票数 0

你好,我在服务器上有一个web服务。我是连接到这个基于BasicHttpBinding的客户端的web服务。我们添加到基于服务cookie的身份验证中,现在我想向我的客户端添加对cookie的支持。

这是我的MyServiceClient课程:

代码语言:javascript
复制
[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);
    }

    ... ...
}

我就是这样创建客户机的:

代码语言:javascript
复制
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实现:

代码语言:javascript
复制
public bool CheckAuthorization()
{
    CheckAuthorizationRequest inValue = new CheckAuthorizationRequest();
    CheckAuthorizationResponse retVal = ((IMyServiceClient)(this)).CheckAuthorization(inValue);
    return retVal.checkAuthorizationResult;
}

我知道曲奇的价值,所以让我们说: string = "blabla";

现在,我需要将这个cookie添加到我的客户端,每当我调用CheckAuthorization时,cookie就会传递。有什么办法吗?我以异常结束“HTTP请求被禁止使用客户端身份验证方案‘Basic’”。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-02 15:50:26

所以我解决了这个问题,我补充道:

代码语言:javascript
复制
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);
}

此外,我必须补充:

代码语言:javascript
复制
var binding = new BasicHttpBinding("MyServiceSoapBinding");
binding.AllowCookies = false;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24530161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档