2011-12-07 36 views
4

我有一个模块化的应用程序,它在单独的AppDomain中实例化事物并通过WCF管道与它们进行通信。我不希望我的流程之外的任何人能够连接到这些管道。内部进程只有WCF命名管道通信?

对此提出建议?

<编辑>我对远程处理知之甚少 - 编写一个在远程处理下使用远程处理的传输是一个可怕的主意吗? < /编辑>

+0

您可以尝试* [NullTransport](http://www.codeproject.com/KB/WCF/NullTransportForWCF.aspx),但这可能是相同的AppDomain-only –

+0

是的,这是相同的AppDomain。 –

+0

命名管道只能在本地机器上使用。您是否担心该机器上的其他应用会尝试访问您的服务? –

回答

2

对不起,我可能会晚点......但迟到总比不到好:) 你可以做的是分享您的应用程序域之间的对象... 例如,在第一个创建一个随机GUID并将其发送到第二个(序列化...)。 那么,如果两个应用程序域知道这个身份验证令牌,你可以做这样的事情:

/// <summary> 
/// Inspect client messages : add GUID in headers 
/// </summary> 
internal class CProcessAuthenticationClientInspector : IClientMessageInspector 
{ 

    #region IClientMessageInspector Membres 

    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) 
    { 
    } 

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) 
    { 
     request.Headers.Add(MessageHeader.CreateHeader("ProcessAuth", "http://schemas.YOURCOMPANY.com/YOURAPPID", CProcessAuthenticationBehavior._authToken)); 
     return null; 
    } 

    #endregion 
} 

/// <summary> 
/// Inspect server messages : Check GUID 
/// </summary> 
internal class CProcessAuthenticationDispatchInspector : IDispatchMessageInspector 
{ 

    #region IDispatchMessageInspector Membres 

    public object AfterReceiveRequest(ref Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) 
    { 
     Guid token = OperationContext.Current.IncomingMessageHeaders.GetHeader<Guid>("ProcessAuth", "http://schemas.YOURCOMPANY.com/YOURAPPID"); 
     if (token != CProcessAuthenticationBehavior._authToken) 
      throw new Exception("Invalid process"); 
     return null; 
    } 

    public void BeforeSendReply(ref Message reply, object correlationState) 
    { 

    } 

    #endregion 
} 

/// <summary> 
/// Add inspectors on both client and server messages 
/// </summary> 
public class CProcessAuthenticationBehavior : IEndpointBehavior 
{ 
    /// <summary> 
    /// Authentification token known by both sides of the pipe 
    /// </summary> 
    internal static Guid _authToken = Guid.NewGuid(); 

    #region IEndpointBehavior Membres 

    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
    { 
    } 

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime) 
    { 
     clientRuntime.MessageInspectors.Add(new CProcessAuthenticationClientInspector()); 
    } 

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher) 
    { 
     endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CProcessAuthenticationDispatchInspector()); 
    } 

    public void Validate(ServiceEndpoint endpoint) 
    { 
    } 

    #endregion 
} 

然后你只需要您的端点行为添加到您的端点双方:

客户端:

ChannelFactory<TInterface> factory; 
factory = new ChannelFactory<TInterface>(BuildLocalBinding(), "net.pipe://localhost/foo"); 
factory.Endpoint.Behaviors.Add(new CProcessAuthenticationBehavior()); 

服务器:

ServiceHost svcHost = new System.ServiceModel.ServiceHost(imlpementationType); 
svcHost.AddServiceEndpoint(interfaceType, binding, "net.pipe://localhost/foo"); 
svcHost.Description.Endpoints[0].Behaviors.Add(new CProcessAuthenticationBehavior()); 

嗯......这可能是在配置完成,但我会让你挖:)

希望这会有所帮助。

+0

我不必实现这一点,但回顾一下我的旧问题,知道我现在知道什么时候,使用在设置时传递给其他域的密钥对中央应用程序域进行身份验证肯定会起作用。 –

1

您可以为绑定添加一些安全行为。它们让您需要验证,签名内容并对其进行加密,具体取决于您的安全需求。

有关更多详细信息,请参阅MSDN上的WCF Security Fundamentals

+0

你能更具体吗?我了解了WCF的这些功能以及它们如何帮助确保它的整体安全性,但我如何才能实现我的特定目标? –

0

... netNamedPipeBinding绑定,它在同一台机器上提供跨进程 通信。命名管道不穿过 机器工作...

NetNamedPipeBinding会实现自己的目标。

NetNamedPipeBinding is optimized for on-machine communication。

+0

命名管道仅限于本地机器吗?你不能调用只通过另一台机器通过管道进行通信的服务,对吧? –

+0

@Terry Donaghe:是的,你是对的,我正在考虑那些过去使用通过tcp代理的命名管道的不幸日子.....不寒而栗。我会相应地更新我的答案。 – Brook

+0

我现在使用的是NetNamedPipeBindings,但是这些对于同一台机器上的用户仍然是开放的。并且我认为注册的管道URI是可发现的。我的意思是,我认为这是一个微不足道的安全问题,但是如果可能的话,我想尽可能地用它做点什么。 –