我已经IDispatchMessageInspector
在我的 WCF 服务中实现了如下所示的安全性。
客户端代码:
private class mySecurityInspector : IClientMessageInspector, IDispatchMessageInspector
{
private const string HEADER_NS = "urn:myservice:authentication-header:2011";
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageHeader<string> messageHeader = new MessageHeader<string>
(getAuthKeyFromConfig(), false, string.Empty, false);
// add the new header to our request
request.Headers.Add(messageHeader.GetUntypedHeader("",
HEADER_NS));
return null;
}
}
服务器端(服务):
private class mySecurityInspector: IDispatchMessageInspector
{
private const string HEADER_NS = "urn:myservice:authentication-header:2011";
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request,
System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext
instanceContext)
{
string authKey = request.Headers.GetHeader<string>(“”, HEADER_NS);
if (authKey != getAuthKeyFromConfig())
{
throw new
Exception(string.Format(ServiceMessages.MSG_UNAUTHORISED_CLIENT,
"");
}
}
}
这样,只有知道 authkey 的客户才能访问我的服务。这就是我所做的。但我不确定安全性,因为我认为如果有人以某种方式从网络线路上的肥皂中破解此 authkey 将能够访问我的服务。在这种情况下如何保护我的服务?请为我提供一个解决方案,以便只有选定的客户才能访问我的服务。