WCF 安全性 之 自定義證書驗證

案例下載html

http://download.csdn.net/detail/woxpp/4113172安全

客戶端調用代碼 經過代理類tcp

代理生成 參見ide

http://www.cnblogs.com/woxpp/p/6232298.htmlspa

X509證書建立.net

http://www.cnblogs.com/woxpp/p/6232325.html代理

服務端配置代碼調試

  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1" behaviorConfiguration="CustomBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/WcfServiceLibrary"/>
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- 除非徹底限定,不然地址將與上面提供的基址相關 -->
        <endpoint address="net.tcp://localhost:8731/WcfServiceLibrary" binding="netTcpBinding" bindingConfiguration="TestNetTcpBinding" contract="WcfServiceLibrary1.IService1">
          <!-- 
              部署時,應刪除或替換下列標識元素,以反映
              用來運行所部署服務的標識。刪除以後,WCF 將
              自動推斷相應標識。
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- 元數據交換終結點供相應的服務用於向客戶端作自我介紹。 -->
        <!-- 此終結點不使用安全綁定,應在部署前確保其安全或將其刪除-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="TestNetTcpBinding">
          <security mode="Transport">
            <transport clientCredentialType="Certificate"/>
            <message clientCredentialType="Certificate"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CustomBehavior">
          <!-- 爲避免泄漏元數據信息,
          請在部署前將如下值設置爲 false 並刪除上面的元數據終結點  -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- 要接收故障異常詳細信息以進行調試,
          請將如下值設置爲 true。在部署前設置爲 false 
            以免泄漏異常信息-->
          <serviceDebug includeExceptionDetailInFaults="False" />
          <serviceCredentials>
            <serviceCertificate findValue="TestServer" storeName="My" storeLocation="CurrentUser" x509FindType="FindBySubjectName"/>
            <clientCertificate>
              <authentication certificateValidationMode="Custom" customCertificateValidatorType="WcfServiceLibrary1.MyX509Validator,WcfServiceLibrary1"/>
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

服務端自定義證書驗證類code

namespace WcfServiceLibrary1
{
    public class MyX509Validator : System.IdentityModel.Selectors.X509CertificateValidator
    {
        public override void Validate(X509Certificate2 certificate)
        {
            if (!certificate.Thumbprint.Equals("B9DF5B912B8CF8EAB07A7BB9B0D17694522AB0CE", StringComparison.CurrentCultureIgnoreCase))
            {
                throw new SecurityTokenException("Unknown Certificate");
            }
        }
    }
}

客戶端調用代碼htm

  private void btnTest_Click(object sender, EventArgs e)
        {
            //Service1Client client = new Service1Client();
            //txtMessage.Text = client.GetDataUsingDataContract(new WcfServiceLibrary1.CompositeType() { StringValue = "sssss" }).StringValue;
             
            NetTcpBinding binding2 = new NetTcpBinding();
            binding2.Security.Mode = SecurityMode.Transport;
            binding2.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
            binding2.Security.Message = new MessageSecurityOverTcp() { ClientCredentialType = MessageCredentialType.Certificate };
            EndpointAddress endpoint = new EndpointAddress(new Uri("net.tcp://localhost:8731/WcfServiceLibrary"),
              EndpointIdentity.CreateDnsIdentity("TestServer"));
            ChannelFactory<IService1> factory = new ChannelFactory<IService1>(binding2, endpoint);
            factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My,
              X509FindType.FindBySubjectName, "TestServer"); 
            IService1 client = factory.CreateChannel();
            txtMessage.Text = client.GetDataUsingDataContract(new WcfServiceLibrary1.CompositeType() { StringValue = "sssss" }).StringValue;
            //B9DF5B912B8CF8EAB07A7BB9B0D17694522AB0CE
        }
相關文章
相關標籤/搜索