2012-01-29 46 views
1

我創建了一個wcf服務,託管它在IIS(7.5),它工作正常。我現在想添加用戶名認證,並遇到一些問題。 配置文件是這樣的:WCF問題 - wsHttpBinding與用戶名認證和TransportWithMessageCredentials

<system.serviceModel> 
    <services> 
     <service behaviorConfiguration="warServBehavior" name="WcfServiceLibrary.WarcraftService"> 
     <endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary.IWarcraftService" bindingConfiguration="warWsHttpBinding" /> 
     <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="warWsHttpBinding"> 
      <security mode="TransportWithMessageCredential"> 
      <transport clientCredentialType="None"/> 
      <message clientCredentialType="UserName"/> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="warServBehavior"> 
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="BogusValidator, App_Code"/> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

關於證書我做了以下(從MSDN啓發):

  1. 1)makecert -n 「CN = RootCATest」 -r -sv RootCATest。 PVK RootCATest.cer

  2. 2)將其添加到受信任的根證書頒發機構

  3. 3)makecert -sk CertTest -iv RootCATest.pvk -n 「CN =虛假」 -ic RootCATest.cer - SR LOCALMACHINE -ss我-sky交換-pe

在IIS我增加了對HTTPS,並在服務器證書我有這個結合: enter image description here

當我運行SvcUtil工具https://localhost/WarcraftServiceSite/WarService.svc我得到這個異常: "There was an error downloading https://localhost/WarcraftServiceSite/WarService.svc. The underlying connection was closed.Could not establish trust relationship for the SSL/TLS secure channel. The remote certificate is invalid according to the validation procedure."

後來編輯:看來,叫SvcUtil工具的正確方法是使用HTTP沒有使用https,即使我有這個<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />

回答

2

因爲這只是一個測試證書,您可以將以下內容添加到客戶端以供其使用。當你從verisign等獲得生產證書時,你不需要這個。 引用和添加用於以下 - System.Net,System.Net.Security,System.Security.Cryptography.X509Certificates;

使用ServicePointManager類和處理程序使用代理服務器在什麼地方添加到ServerCertificateValidationCallback

ServicePointManager.ServerCertificateValidationCallback 
       += RemoteCertificateValidate; 

然後處理IMPL

private static bool RemoteCertificateValidate(
    object sender, X509Certificate cert, 
    X509Chain chain, SslPolicyErrors error) 
{ 
    // trust any certificate 
    return true; 
} 

線了處理程序。 記住這個代碼和makecert的證書只能用於測試。