WCF Security of Windows and Certificate

WFC 安全性
WCF安全性主要是依靠bindingsbehaviors兩個配置節設置的。因爲binding可以綁定的協議很多,因此設置安全性的組合和很多,下面是設置netTcpBinding 綁定的windows驗證和證書驗證的兩種方式,因爲netTcpBindingIntranet中傳輸效率最高的,他的默認modetransport,該模式是對當前的信道加密。也可以該他的mode,通常如果是internet的話將mode改爲message方式,這種方式是對消息加密或簽名,可以具體到沒有個方法加密和簽名,因爲有些非敏感的數據是不需要加密的。當IISWCF不是在同一臺機器上的時候是需要設置安全性的,否則WCF是拒絕提供服務的,netTcpBinding的默認驗證方式是windows驗證,因爲如果不設置的話是訪問不了的。下面分別是兩種方式的實現
第一種 windows驗證
    Windows驗證主要是需要驗證當前請求服務的憑證裏面是否提供當前WCF運行系統的用戶名和密碼,如果提供的用戶名和密碼不正確是訪問不了服務的,設置憑證的方式是            tcpChannel.Credentials.Windows.ClientCredential =

                new System.Net.NetworkCredential("userName", "password", "domain");

主要提供用戶名,密碼和該用戶所隸屬於的域,相對方式比較簡單。這種方式需要知道WCF服務所在機器的用戶名和密碼,相對是存在一定風險的。

配置文件

第二種certificate驗證

     Certificate驗證是有WCF服務端產生一個證書,然後分給所有需要訪問該服務的終端,只要有這個證書的客戶端才能訪問服務,這種方式主要用於internet方式。實現主要是增加behaviors配置節。

客戶端的配置節:

<system.serviceModel>

    <behaviors>

      <endpointBehaviors>

        <behavior name="CertificateBehavior">

          <clientCredentials>

--設置客戶端憑證 findvalue:在當前用戶證書中所要找的證書名稱

storeName:證書方的位置,個人證書放在My

storeLocation:證書存放在當前用戶下或是本機下

x509FindType:設置根據對象名稱來找證書

            <clientCertificate findValue="Client1" storeName="My" storeLocation="CurrentUser" x509FindType="FindBySubjectName"/>

--設置服務端憑證,在這沒有對服務端設置驗證

            <serviceCertificate>

              <authentication certificateValidationMode="None"/>

            </serviceCertificate>

          </clientCredentials>

        </behavior>

      </endpointBehaviors>

    </behaviors>

    <client>

      <endpoint  address="net.tcp://192.168.30.65:9002/TcpOBFService"  behaviorConfiguration="CertificateBehavior" binding="netTcpBinding" bindingConfiguration="netTcpEndPoint"  contract="OBF.Service.IOBFService" name="DefaultOBFService">

        <identity>

          <dns value="MyServer"/>

        </identity>

      </endpoint>

    </client>

     <bindings >

      <netTcpBinding  >

        <binding name="netTcpEndPoint"  maxReceivedMessageSize="10000000" sendTimeout="00:03:00"  >

          <security mode="Transport" >

-- clientCredentialType:設置驗證的方式 Authentication

-- protectionLevel:設置信息的簽名和加密,這樣信息不會以明文傳輸

            <transport clientCredentialType="Certificate" protectionLevel="EncryptAndSign" />

          </security>

        </binding>

      </netTcpBinding>

    </bindings>

  </system.serviceModel>

服務器配置節:

<system.serviceModel>

    <services>      <service  name="OBF.Service.OBFService" behaviorConfiguration="MyServiceTypeBehaviors">

        <host>

          <baseAddresses>

            <add baseAddress="http://192.168.30.65:9000/OBFService"/>

          </baseAddresses>

        </host>

        <endpoint contract="OBF.Service.IOBFService"  binding="netTcpBinding" bindingConfiguration="netTcpEndPoint" address="net.tcp://localhost:9002/TcpOBFService"/>

      </service>

    </services>

    <bindings>

       <netTcpBinding>

        <binding name="netTcpEndPoint" maxReceivedMessageSize="10000000" sendTimeout="00:03:00"  >

          <security mode="Transport">

            <transport clientCredentialType="Certificate" protectionLevel="EncryptAndSign"/>

          </security>

        </binding>

      </netTcpBinding>

    </bindings>

    <behaviors>

      <serviceBehaviors>

        <behavior name="MyServiceTypeBehaviors">

          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://192.168.30.65:9001/OBFService"/>

          <serviceDebug includeExceptionDetailInFaults="true"/>

          <serviceCredentials>

            <clientCertificate>   <!—指定服務器驗證的類-->

              <authentication certificateValidationMode="Custom" customCertificateValidatorType="OBF.TestServiceHost.CustomX509CertificateValidator,OBF.TestServiceHost"/>

            </clientCertificate>

            <serviceCertificate findValue="MyServer" storeName="My" storeLocation="CurrentUser" x509FindType="FindBySubjectName"/>

          </serviceCredentials>

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章