2011-09-16 10 views
3

自己署名付き証明書(inetmgrで生成)を使用して、WCF 4でサービスアプリケーションを保護したいとします。証明書を使用したIISでのセキュアなWCFサービス

しかし、私はできません。サービスのメソッドを呼び出すと、MessageSecurityExceptionが発生します。

クライアント認証スキーム「Anonymous」でHTTPリクエストが禁止されました。

web.configファイル:

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
     <compilation debug="true" targetFramework="4.0" /> 
     <customErrors mode="Off"/> 
    </system.web> 

    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="TransportSecurity"> 
        <security mode="TransportWithMessageCredential"> 
         <transport clientCredentialType="Certificate" /> 
         <message clientCredentialType="Certificate"/> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 

     <behaviors> 
      <serviceBehaviors> 
       <behavior name="testingServiceBehavior"> 
        <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" /> 
        <serviceDebug includeExceptionDetailInFaults="false"/> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 

     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 

     <services> 
      <service behaviorConfiguration="testingServiceBehavior" 
        name="Testing.Service1"> 

       <endpoint address="" 
          binding="wsHttpBinding" 
          bindingConfiguration="TransportSecurity" 
          contract="Testing.IService1" /> 

       <endpoint address="mex" 
          binding="mexHttpsBinding" 
          contract="IMetadataExchange" /> 
      </service> 
     </services> 
    </system.serviceModel> 

    <system.webServer> 
     <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

そして、私はサービスを消費しようとしているコードは次のとおりです。

public static bool validateCertificates(object sender, 
              System.Security.Cryptography.X509Certificates.X509Certificate cert, 
              System.Security.Cryptography.X509Certificates.X509Chain chain, 
              System.Net.Security.SslPolicyErrors error) 
    { 
     return true; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(validateCertificates); 

     WSHttpBinding binding = new WSHttpBinding(); 
     binding.Name = "secureBinding"; 

     binding.Security.Mode = SecurityMode.TransportWithMessageCredential; 
     binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate; 
     binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; 

     EndpointAddress endpointAddress = new System.ServiceModel.EndpointAddress("https://rtsa.dnsalias.com:2490/Service1.svc"); 

     ProCell2.Servicios.Informes.Service1Client client = new Servicios.Informes.Service1Client(binding, endpointAddress); 

     client.ClientCredentials.ClientCertificate.SetCertificate(
       StoreLocation.CurrentUser, 
       StoreName.My, 
       X509FindType.FindBySubjectName, 
       "ServerWeb2"); 

     client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(
       StoreLocation.CurrentUser, 
       StoreName.My, 
       X509FindType.FindBySubjectName, 
       "ServerWeb2"); 

     client.GetInformation(); // <-------- Here cause the exception 

SSLの設定:

SSL Setting

+0

IISのどのバージョンから実行していますか? SSLを有効にしていますか? – kroonwijk

+0

iis 7.0.6で実行されていて、sslが有効な場合、iisのバインディングには同じ証明書があります。 – Gabriel

答えて

0

クライアントコードに次の行を追加してください:

// Disable credential negotiation and the establishment of 
// a security context. 
myBinding.Security.Message.NegotiateServiceCredential = false; 
myBinding.Security.Message.EstablishSecurityContext = false; 

は詳細についてはhttp://msdn.microsoft.com/en-us/library/ms733102.aspxを参照してください、それはあなたのコミュニケーションに与える影響のためWhat are the impacts of setting establishSecurityContext="False" if i use https?を参照してください。

+0

あなたの助けに感謝しますが、それでも同じエラーです。さえ、私は試してweb.configに入れます。 – Gabriel

+0

そして、http://msdn.microsoft.com/en-us/library/ms733102.aspxで入手可能なexacteサンプルを実行すると、同じエラーが表示されますか?問題はクライアント側であり、明らかにクライアントコードの変更である必要があります。または、別の可能性。サーバー側の信頼できるルート権限リストにログインしようとしている証明書がありますか?そうしないと、チャレンジにその証明書ルートが含まれず、クライアントは要求に応じてクライアントを送信できなくなります。 – kroonwijk

関連する問題