0

自己署名証明書を使用して WCF を実行しようとしています。いくつかの解決策を見つけましたが、接続前にクライアントがクリネット証明書を知る必要があります。クライアントをブラウザとして動作させたい(ブラウザがWebサーバー証明書を使用している場合)。つまり、ブラウザーは接続前に Web サーバーの証明書を認識していないため、Web サーバーから証明書を取得できます。私のサーバーの web.config:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
    <services>
      <service behaviorConfiguration="security" name="WebApplicationExchange.UKService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="security" contract="WebApplicationExchange.IUKService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost/UKService/UKService.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="security">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="ServiceAuthorization.PasswordValidator,ServiceAuthorization" />
            <windowsAuthentication allowAnonymousLogons="false" />
          </serviceCredentials>
          <serviceAuthorization principalPermissionMode="Custom">
            <authorizationPolicies>
              <add policyType="ServiceAuthorization.AuthorizationPolicy,ServiceAuthorization" />
            </authorizationPolicies>
          </serviceAuthorization>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="security">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="Basic" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>

Web ブラウザでは正常に動作します。

しかし、クライアントにはエラーが表示されます: 権限 "localhost: 56389" でセキュア チャネル SSL / TLS の信頼接続を確立できませんでした。クライアントコード:

System.ServiceModel.EndpointAddress eaSecurity = new System.ServiceModel.EndpointAddress("https://localhost:56389/UKService.svc");
                var binding = new System.ServiceModel.BasicHttpBinding();
                binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportWithMessageCredential;
                binding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
                binding.Security.Message.ClientCredentialType = System.ServiceModel.BasicHttpMessageCredentialType.UserName;


                var securityFactory = new System.ServiceModel.ChannelFactory<ServiceReference1.IUKService>(binding, eaSecurity);
                securityFactory.Credentials.UserName.UserName = "test";
                securityFactory.Credentials.UserName.Password = "test";
                myService = securityFactory.CreateChannel();

これを解決する方法はありますか?

更新: エラー スタックを詳しく調べたところ、次のことがわかりました: System.Security.Authentication.AuthenticationException: 認証の結果によると、リモート証明書が無効です。私はこのコードを試しました:

  ClientCredentials cc = securityFactory.Endpoint.Behaviors.Find<ClientCredentials>();

                cc.UserName.UserName = "test";
                cc.UserName.Password = "test";

               cc.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

しかし、それは役に立ちません。

4

2 に答える 2

1

私はこのように解決します:

System.Net.ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidateCallback);
 public static bool RemoteCertValidateCallback(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
        {
            return true;
        }
于 2012-11-27T07:56:01.663 に答える
0

問題は、証明書がクライアントによって信頼されていないことです。これは、クライアント証明書やブラウザからの証明書のダウンロードとは関係ありません。

サーバー証明書がクライアントによって受け入れられるようにするには、次のいずれかを行う必要があります。

  • 信頼できる認証局から証明書を購入する
  • 証明書 (またはそのルート) をクライアント マシンの証明書ストアに追加するか、
  • 証明書の検証を無効にします。
于 2012-11-26T10:49:41.023 に答える