我使用 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.
发布评论
评论(1)
我有一个解决方案,我创建 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