Can';无法读取WCF服务中的标头
我正在尝试添加一个自定义安全功能,其中客户端向Silverlight客户端发出的每个服务调用添加一个令牌,然后服务可以访问该令牌来控制应用程序安全系统。我试图通过实现IClientMessageInspector接口并将其链接到生成的服务客户端来实现这一点。我使用的不是Visual studio生成的代理,而是由ChannelFactory创建的我自己的客户端。我在网上找到了几种解决方案,似乎都使用了两种基本方法中的一种。首先,向IClientMessageInspector.BeforeSendRequest提供的消息的headers集合添加标头,然后使用HttpRequestMessageProperty将信息作为属性添加到IClientMessageInspector.BeforeSendRequest提供的消息中。我一直在尝试这两种方法,但都没有成功。这两种技术似乎都成功地将数据添加到请求中,但我无法在服务器上访问这两种技术。我想补充一点,这对我来说是一个非常新的领域,我很可能因为缺乏经验而错过了互联网上的答案 生成我的客户端的代码是:Can';无法读取WCF服务中的标头,wcf,http-headers,Wcf,Http Headers,我正在尝试添加一个自定义安全功能,其中客户端向Silverlight客户端发出的每个服务调用添加一个令牌,然后服务可以访问该令牌来控制应用程序安全系统。我试图通过实现IClientMessageInspector接口并将其链接到生成的服务客户端来实现这一点。我使用的不是Visual studio生成的代理,而是由ChannelFactory创建的我自己的客户端。我在网上找到了几种解决方案,似乎都使用了两种基本方法中的一种。首先,向IClientMessageInspector.BeforeSen
private ISecurityAdministrationContract CreateChannel()
{
if (factory == null)
{
lock (this)
{
// Create a custom binding that uses HTTP and binary encoding.
var elements = new List<BindingElement>();
elements.Add(new BinaryMessageEncodingBindingElement());
elements.Add(new HttpTransportBindingElement());
var binding = new CustomBinding(elements);
// Create a channel factory for the service endpoint configured with the custom binding.
factory = new ChannelFactory<ISecurityAdministrationContract>(binding, new EndpointAddress(SecurityAdminServiceAddress));
//Add my IClientMessageInspector
factory.Endpoint.Behaviors.Add(new ClientSecurityInterceptor());
}
}
ISecurityAdministrationContract client = factory.CreateChannel();
return client;
}
}
ISecurityAdministrationContract client = factory.CreateChannel();
return client;
}
上面的代码实现了这两种技术
fiddler捕获的服务调用是:(NB http替换为ht_tp,因为该站点不允许我发布hyoerlinks)
POST ht_tp://127.0.0.1:6785/SecurityAdministrationRelayService.svc/HTTP/1.1
接受:/
推荐人:ht_tp://ipv4.fiddler:6785/ClientBin/Civica.Housing.xap
接受语言:en gb
内容长度:400
内容类型:应用程序/soap+msbin1
MySecondAuthentication:11111111-2222-3333-4444-55555
接受编码:gzip,deflate
用户代理:Mozilla/4.0(兼容;MSIE 8.0;Windows NT 5.1;Trident/4.0;GTB5;InfoPath.2;.NET CLR 3.0.04506.648;.NET CLR 3.5.21022;.NET CLR 3.0.4506.2152;.NET CLR 3.5.30729;.NET4.0C;.NET4.0E;.NET CLR 2.0.50727)
主持人:127.0.0.1:6785
连接:保持活力
Pragma:没有缓存
五_
s
_a_V_D
���A.http://tempuri.org/ISecurityAdministrationContract/CreateAdvocateD�ߔ_�9�HJ��9��-��D、 D*�@MyFirstAuthentication\n�%AAAAAAAA-BBBB-CCCC-DDDD-eeeeed
_���@http://ipv4.fiddler:6785/SecurityAdministrationRelayService.svc/V@_创意倡导者_http://tempuri.org/@
提倡�{“Id”:0,“UserId”:4,“AdvocateForId”:8,“ValidFrom”:“/Date(12911278690+0000)/”,“ValidTo”:null}\uuuu
这似乎包含了这两种技术的令牌信息
当我试图提取此信息时,问题出现在服务器上。我已经实现了一个IOperationInvoker,并尝试在IncomingMessageHeaders中查找头,但没有成功。我还搜索了IncomingMessageProperties,但我看不到任何添加的标题详细信息。
这是我在IOperationInvoker中使用的代码:
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
PreInvoke(instance, inputs);
object returnedValue = null;
object[] outputparams = new object[] { };
Exception exception = null;
try
{
returnedValue = originalInvoker.Invoke(instance, inputs, out outputparams);
outputs = outputparams;
return returnedValue;
}
catch (Exception e)
{
exception = e; throw;
}
finally
{
PostInvoke(instance, returnedValue, outputparams, exception);
}
}
protected virtual void PreInvoke(object instance, object[] inputs)
{
//Look for header directly
int index = System.ServiceModel.OperationContext.Current.IncomingMessageHeaders.FindHeader("MyFirstAuthentication", "ns");
//Search via enumerator
foreach (var header in System.ServiceModel.OperationContext.Current.IncomingMessageHeaders)
{
}
}
FindHeader返回-1,而枚举数查找5个标头,即;'操作“,”MessageID“,”ReplyTo“,”VsDebuggerCausalityData“,”To“
OperationContext.Current.IncomingMessageProperties集合包含4个条目,即:“Via”、“Security”、“Encoder”和“System.ServiceModel.Channels.RemoteEndpointMessageProperty”
事实上,如果我在客户端中注释掉将IClientMessageInspector添加到ClientChannel的那一行,那么fiddler报告的http将通过省略添加的详细信息而发生更改,但传入消息的头和属性集合将保持不变
如果您有任何关于我如何访问此信息的想法,或者为什么不将其作为收入信息的一部分提供,我们将不胜感激。我已经回答了自己的问题。上面的任何代码都没有问题,只需要将其链接到正确的位置。在我的完整解决方案中,我从Silverlight客户端调用一个服务,然后该客户端调用第二个服务。令人尴尬的是,我将iOperationVoker链接到了第二个服务(doh)。当我将它链接到中间服务时,标题正如预期的那样可用 读取该值的代码为:
int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("MyFirstAuthentication", "ns");
if (index >= 0)
{
string value = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);
}
int index=OperationContext.Current.IncomingMessageHeaders.FindHeader(“MyFirstAuthentication”、“ns”);
如果(索引>=0)
{
字符串值=OperationContext.Current.IncomingMessageHeaders.GetHeader(索引);
}
您可以使用以下命令访问标题值:
var headers = OperationContext.Current.IncomingMessageProperties["httpRequest"];
var apiToken = ((HttpRequestMessageProperty)headers).Headers["<YourHeaderKey>"];
var headers=OperationContext.Current.IncomingMessageProperties[“httpRequest”];
var apiToken=((HttpRequestMessageProperty)头)。头[“”];
较短版本:
string value = OperationContext.Current.
IncomingMessageHeaders.GetHeader<string>("MyFirstAuthentication", "ns");
string value=OperationContext.Current。
IncomingMessageHeaders.GetHeader(“MyFirstAuthentication”、“ns”);
string value = OperationContext.Current.
IncomingMessageHeaders.GetHeader<string>("MyFirstAuthentication", "ns");