2009-08-18 8 views
7

WCFサービスの一部のエンドポイントを保護する必要があります。エンドポイントを保護できるかどうかはわかりません。私は以下のWCFサービス(自己ホスト)を持っています。同じWCFがCAポリシーファイルにも対応しています。このWCFサービスまたはいくつかのエンドポイントのセキュリティを確保した場合、CAポリシーパートはユーザー名パスワードを要求してはなりません。ポリシーファイルは常にアクセス可能でなければなりません。それも可能ですか?カスタム認証でWCFサービスエンドポイントを保護する

私は多くのWCFカスタムブログ/投稿を見つけました。セキュリティを行う方法はたくさんあります。私が望むのは、ユーザー名/パスワードでいくつかのエンドポイントを保護できるということですが、Fiddlerのようなツールでは資格情報を表示してはいけません。ただし、この場合はデータが表示されます。

私はすでにCustomvalidatorを実装していますが、app.configファイルもインポート対象です。そして私はそれをあまりよくしていません。

namespace WindowsFormsApplication11 
{ 
    public partial class Form1 : Form 
    { 
     public ServiceHost _host = null; 

     public Form1() 
     { 
      InitializeComponent(); 
     }  

     private void button1_Click(object sender, EventArgs e) 
     { 
      // Create a ServiceHost for the CalculatorService type and 
      // provide the base address. 
      _host = new ServiceHost(typeof(WmsStatService)); 
      _host.AddServiceEndpoint(typeof(IPolicyProvider), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); 

      _host.Open(); 
     } 
    } 

    // Define a service contract. 
    [ServiceContract(Namespace = "http://WindowsFormsApplication11")] 
    public interface IWmsStat 
    { 
     [OperationContract] 
     string getConnectedViewers(string channelName); 
     [OperationContract] 
     string sayHello(string name); 
    } 

    [ServiceContract] 
    public interface IPolicyProvider 
    { 
     [OperationContract, WebGet(UriTemplate = "/ClientAccessPolicy.xml")] 
     Stream ProvidePolicy(); 
    } 
    //[DataContract] 
    public class Ads 
    { 
     // [DataMember] 
     public string AdFileName { get; set; } 
     //[DataMember] 
     public string AdDestenationUrl { get; set; } 
     public string ConnectedUserIP { get; set; } 
    } 
    // 
    public class CustomValidator : UserNamePasswordValidator 
    { 
     public override void Validate(string userName, string password) 
     { 
      if(null == userName || null == password) 
      { 
        throw new ArgumentNullException(); 
      } 
      if(userName == "Oguz" && password == "2009") 
      { 
       return; 
      } 
      FaultCode fc = new FaultCode("ValidationFailed"); 
      FaultReason fr = new FaultReason("Good reason"); 
      throw new FaultException(fr,fc); 
     } 
    } 
    // 

    public class WmsStatService : IWmsStat, IPolicyProvider 
    { 
     public string sayHello(string name) 
     { 
      return "hello there " + name + " nice to meet you!"; 
     } 

     public Stream ProvidePolicy() 
     { 
      WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml"; 
      return new MemoryStream(File.ReadAllBytes("ClientAccessPolicy.xml"), false); 
     } 

     public string getConnectedViewers(string channelname) 
     { 
      // do stuff 
      return null; 
     } 
    } 
} 

app.config。この設定ファイルは機能しません。エンドポイントのカスタム認証を入れたいと思っていました。私は見当もつかない。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <services> 
     <service name="WindowsFormsApplication11.WmsStatService" behaviorConfiguration="mex"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://192.168.0.199:87" /> 
      </baseAddresses> 
     </host>   
     <endpoint address="http://192.168.0.199:87/Test" binding="basicHttpBinding" bindingConfiguration="" contract="WindowsFormsApplication11.IWmsStat" behaviorConfiguration="MyServiceBehavior" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 

    <!--<bindings> 
     <wsHttpBinding>  
     <binding name="wshttp"> 
      <security mode="Message"> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings>--> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="mex"> 
      <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> 
     </behavior> 
     <behavior name="MyServiceBehavior"> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WindowsFormsApplication11.CustomValidator, CustomValidator" /> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors>  
    </behaviors> 
    </system.serviceModel> 
</configuration> 

答えて

17

、私はあなたが 、いくつかのエンドポイントと、いくつかのないを確保できるかどうか知りません。

確かに - あなたはちょうど2つの別々の結合構成を作成する必要があり、他の人の上に固定されているこれらのエンドポイント上の1、他を使用します。

<bindings> 
    <basicHttpBinding> 
    <binding name="secured"> 
     <security mode="Message"> 
     <message ...... /> 
     </security> 
    </binding> 
    <binding name="unsecured"> 
     <security mode="None" /> 
    </binding> 
    </basicHttpBinding> 
</bindings> 
<services> 
    <service name="WindowsFormsApplication11.WmsStatService" behaviorConfiguration="mex"> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://192.168.0.199:87" /> 
     </baseAddresses> 
    </host>   

    <endpoint address="/Secured/Test" 
       binding="basicHttpBinding" bindingConfiguration="secured" 
       contract="WindowsFormsApplication11.IWmsStat" 
       behaviorConfiguration="MyServiceBehavior" /> 

    <endpoint address="/Unsecured/Test" 
       binding="basicHttpBinding" bindingConfiguration="unsecured" 
       contract="WindowsFormsApplication11.IWmsStat" 
       behaviorConfiguration="MyServiceBehavior" /> 

    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 

マルク・

PS:わかりませんあなたの投稿がもう最新ではないという問題がある場合は、2つの別個の行動設定があることに気づいたでしょうか?

<behaviors> 
    <serviceBehaviors> 
     <behavior name="mex"> 
     <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> 
     </behavior> 
     <behavior name="MyServiceBehavior"> 
     <serviceCredentials> 
      <userNameAuthentication 
       userNamePasswordValidationMode="Custom" 
       customUserNamePasswordValidatorType="WindowsFormsApplication11.CustomValidator, CustomValidator" /> 
     </serviceCredentials> 
     </behavior> 
    </serviceBehaviors>  
</behaviors> 

あなたのサービスは「mex」の動作のみを参照していますか?つまり、あなたのサービスは実際に<serviceMetadata>の動作を使用していますが、NOT<serviceCredentials>です。

あなたは一つにこれらをマージしてからちょうどそれを参照する必要があります。

<behaviors> 
    <serviceBehaviors> 
     <behavior name="Default"> 
     <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> 
     <serviceCredentials> 
      <userNameAuthentication 
       userNamePasswordValidationMode="Custom" 
       customUserNamePasswordValidatorType="WindowsFormsApplication11.CustomValidator, CustomValidator" /> 
     </serviceCredentials> 
     </behavior> 
    </serviceBehaviors>  
</behaviors> 
<services> 
    <service name="...." behaviorConfiguration="Default" 

マルク・

+0

私は今、これを取得しています:( は、ファイルまたはアセンブリ「のCustomValidator」またはその依存関係の1つをロードできませんでした。システムは指定されたファイルを見つけることができませんくそ – Shift

+0

エラーがここにこの設定から来ている:。。

+0

を実行するとuserNamePasswordValidationMode = "カスタム" customUserNamePasswordValidatorType = "WindowsFormsApplication11.CustomValidator、のCustomValidator" /> は、私がWindowsFormsAppiication11に。これは、WCFホストされた自己であることに変更可能なこと "のCustomValidator" アセンブリです私はそれを変更した後、エラーは消えてしまったが、別の問題:( ChannelDispatcher 'http://192.168.0.199:87/Test' with contract(s) 'IWmsStat' 'がIChannelListenerを開くことができません – Shift

2

メッセージ全体を保護する場合は、トランスポートセキュリティモードを使用します。ヘッダーのみを暗号化/署名する場合は、メッセージセキュリティモードで許可しますが、wsHttpBindingを使用する必要があります。ダイジェストを使用して資格情報を保護することも考えられます。

あなたの例については、私はあなたのコメントの部分は次のようになりますと思う:

<bindings> 
    <basicHttpBinding> 
      <binding name="secure"> 
     <security mode="Transport"> 
     <transport clientCredentialType="Basic" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

あなたのエンドポイントの宣言を更新することもあります:

<endpoint 
    address="https://192.168.0.199:87/Test" 
    binding="basicHttpBinding" bindingConfiguration="secure" 
    contract="WindowsFormsApplication11.IWmsStat" /> 

あなたが許可されませんトランスポートセキュリティモードでプレーンHTTPを使用する。私は WCFサービスのいくつかのエンドポイントを保護する

+0

私は、次のエラーを取得していますそのような構成と。 'MyServiceBehavior'というエンドポイントの動作はありません – Shift

+0

httpsを使用したいのでメッセージセキュリティモードを選択しません – Shift

+0

サービスタグにbehaviorConfiguration = "CalculatorServiceBehavior"を移動 –

関連する問題