使用 ChannelFactory 通过 WCF 发送 Cookie

发布于 2024-09-28 11:46:35 字数 1388 浏览 6 评论 0 原文

我使用 IOC 容器为我提供 IService。 如果 IService 是 WCF 服务,则它由通道工厂提供 当 IService 位于同一台计算机上时,它能够访问相同的 cookie,因此没有问题,但是一旦调用 WCF 服务,就需要发送这些 cookie。

我花了很多时间试图找到一种使用通道工厂发送 cookie 的方法,我发现有效的唯一方法是以下

var cookie = _authenticationClient.GetHttpCookie();
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers.Add(HttpRequestHeader.Cookie, cookie.Name + "=" + cookie.Value);
using(var scope = new OperationContextScope((IClientChannel)_service))
{
   OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
   var result = _service.Add(details);
   if (result.Result == RPGResult.Success)
   {
       return RedirectToAction("Index", "Home", result.Id);
   }
}

方法我使用该方法的问题是我必须知道我正在调用WCF 服务,但情况并非总是如此。我尝试为 ChannelFactory 编写一个包装器,在创建新服务和各种其他解决方案时打开一个新的操作上下文范围,但没有任何效果。

有人有通过 WCF 服务发送 cookie 的经验吗?

我找到了一个涉及使用 SilverLight 的解决方案,不幸的是我没有使用 silverlight,解决方案在这里: http://greenicicleblog.com/2009/10/27/using-the-silverlight-httpclient-in-wcf-and-still-passing-cookies / 不幸的是标准.net不包含IHttpCookieContainerManager接口

理想情况下我将能够使用类似的东西,即我将能够告诉channelfactory在打开时传递一个cookie。

如果有人有更好的方法来传递用于身份验证的令牌,我们也将不胜感激。

I use an IOC container which provides me with IService.
In the case where IService is a WCF service it is provided by a channel factory
When IService lives on the same machine it is able to access the same cookies and so no problem however once a WCF Service is called it needs to be sent those cookies.

I've spent a lot of time trying to find a way to send cookies using a channel factory and the only way I could find that works is the following

var cookie = _authenticationClient.GetHttpCookie();
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers.Add(HttpRequestHeader.Cookie, cookie.Name + "=" + cookie.Value);
using(var scope = new OperationContextScope((IClientChannel)_service))
{
   OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
   var result = _service.Add(details);
   if (result.Result == RPGResult.Success)
   {
       return RedirectToAction("Index", "Home", result.Id);
   }
}

The problem with me using that method is that I have to know that I'm calling a WCF Service which is not always the case. I've tried writing a wrapper for the ChannelFactory that opens a new operationcontextscope when it creates a new service and various other solutions but nothing has worked.

Anyone have any experience with sending cookies over WCF Services?

I found a solution involving using SilverLight, unfortunately I'm not using silverlight, the solution is here: http://greenicicleblog.com/2009/10/27/using-the-silverlight-httpclient-in-wcf-and-still-passing-cookies/
Unfortunately standard .net doesn't contain the IHttpCookieContainerManager interface

Ideally I would be able to use something similar,i.e. I would be able to tell the channelfactory to pass a cookie whenever it opened.

If anyone has a better way to pass a token that is used for authentication that would be appreciated too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-10-05 11:46:35

我有一个解决方案,我创建 IService 的代理类,然后每次调用 IService 上的方法时,它都会调用通道工厂创建的代理,但调用本身包装在操作上下文范围中,就像我在问题中所遇到的那样。
我使用此链接中的代理工厂 http://www.codeproject.com/KB/cs /dynamicproxy.aspx

I have a solution where I create a proxy class of IService and then every time a method on IService is called it invokes the proxy created by the channel factory but the call itself is wrapped in an operationcontextscope just like the one I have in my question.
I used the proxy factory from this link http://www.codeproject.com/KB/cs/dynamicproxy.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文