我正在尝试添加自定义安全功能,其中客户端为Silverlight客户端发出的每个服务调用添加一个令牌,然后该服务可以访问此令牌来控制应用程序安全系统.我试图通过实现IClientMessageInspector接口并将其链接到我生成的服务客户端来实现此目的.我没有使用Visual Studio生成的代理,而是使用ChannelFactory创建的自己的客户端.我已经设法在网上找到了几个似乎都使用两种基本方法之一的解决方案.首先,通过向呈现给IClientMessageInspector.BeforeSendRequest的Message的headers集合添加标头,其次通过使用HttpRequestMessageProperty将信息作为属性添加到呈现给IClientMessageInspector.BeforeSendRequest的Message.我一直在尝试这两种方法而没有任何成功.看来两种技术都成功地将数据添加到请求中,但我无法在服务器上访问.我想补充一点,对我来说这是一个非常新的领域,由于缺乏经验,我很可能在互联网上错过了答案.
生成我的客户端的代码是:
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 …Run Code Online (Sandbox Code Playgroud)