2011-04-19 16 views
1

私はさまざまな.NETアプリケーション間の通信にWCFを使用しています。これらのサービスはすべて同じプライベートサブネット上にあるため、暗号化と証明書の複雑さとパフォーマンスのオーバーヘッドを避けたいと考えています。私は、しかし、すべてのリクエストは私たちのカスタムMembershipProviderに対して認証されているので、基本的なユーザー名/パスワードのサポートが必要です。ユーザー認証と証明書なしのWCFのTCP

私たちは現在Clear Username BindingでHTTPを使用しており、うまくいきます。しかし、パフォーマンスを向上させるためにTCPを使用したいと思います。証明書や暗号化などを使わなくても簡単なユーザ名/パスワード認証(Clear Username Bindingの場合)をNetTcpBinding以上にすることはできますか?

答えて

5

最後に解決策は、Clear Username Bindingを変更して、トランスポートとバイナリメッセージのエンコードにTCPを使用することでした。私はseries of commentsのアイディアをthe author's blogに持っています。私のバインディングの完全なコードは以下の通りです:

using System; 
using System.Configuration; 
using System.Net.Security; 
using System.ServiceModel.Channels; 
using System.ServiceModel.Configuration; 

namespace ClearTcpBinding 
{ 
    public class ClearTcpBinding : CustomBinding 
    { 
     private long _maxReceivedMessageSize = 65536; 

     public void SetMaxReceivedMessageSize(long value) 
     { 
      _maxReceivedMessageSize = value; 
     } 

     public override BindingElementCollection CreateBindingElements() 
     { 
      var res = new BindingElementCollection 
          { 
           new BinaryMessageEncodingBindingElement {MessageVersion = MessageVersion.Soap12WSAddressing10}, 
           SecurityBindingElement.CreateUserNameOverTransportBindingElement(), 
           new AutoSecuredTcpTransportElement {MaxReceivedMessageSize = _maxReceivedMessageSize} 
          }; 
      return res; 
     } 

     public override string Scheme { get { return "net.tcp"; } } 
    } 

    public class ClearTcpBindingElement : StandardBindingElement 
    { 
     private ConfigurationPropertyCollection _properties; 

     protected override void OnApplyConfiguration(Binding binding) 
     { 
      var b = (ClearTcpBinding)binding; 
      b.SetMaxReceivedMessageSize(Convert.ToInt64(MaxReceivedMessageSize)); 
     } 

     protected override Type BindingElementType 
     { 
      get { return typeof(ClearTcpBinding); } 
     } 

     protected override ConfigurationPropertyCollection Properties 
     { 
      get 
      { 
       if (_properties == null) 
       { 
        var properties = base.Properties; 
        properties.Add(new ConfigurationProperty("maxReceivedMessageSize", typeof(string), "65536")); 
        _properties = properties; 
       } 
       return _properties; 
      } 
     } 

     public string MaxReceivedMessageSize 
     { 
      get { return (string)base["maxReceivedMessageSize"]; } 
      set { base["maxReceivedMessageSize"] = value; } 
     } 
    } 

    public class ClearTcpCollectionElement 
     : StandardBindingCollectionElement<ClearTcpBinding, ClearTcpBindingElement> 
    { 
    } 

    public class AutoSecuredTcpTransportElement : TcpTransportBindingElement, ITransportTokenAssertionProvider 
    { 
     public override T GetProperty<T>(BindingContext context) 
     { 
      if (typeof(T) == typeof(ISecurityCapabilities)) 
       return (T)(ISecurityCapabilities)new AutoSecuredTcpSecurityCapabilities(); 
      return base.GetProperty<T>(context); 
     } 

     public System.Xml.XmlElement GetTransportTokenAssertion() 
     { 
      return null; 
     } 
    } 

    public class AutoSecuredTcpSecurityCapabilities : ISecurityCapabilities 
    { 
     public ProtectionLevel SupportedRequestProtectionLevel { get { return ProtectionLevel.EncryptAndSign; } } 
     public ProtectionLevel SupportedResponseProtectionLevel { get { return ProtectionLevel.EncryptAndSign; } } 
     public bool SupportsClientAuthentication { get { return false; } } 
     public bool SupportsClientWindowsIdentity { get { return false; } } 
     public bool SupportsServerAuthentication { get { return true; } } 
    } 
}