2011-08-10 308 views
0

是否需要創建服務證書以使用自定義用戶名和密碼身份驗證?我想用自定義用戶名和密碼來驗證我的WCF服務。WCF身份驗證:自定義用戶名和密碼驗證程序asp.net

我服務的web.config如下:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding>`enter code here` 
      <binding name="NewBinding0"> 
       <security mode="Message"> 
        <transport clientCredentialType="Basic" /> 
        <message clientCredentialType="UserName" /> 
       </security> 
      </binding> 
     </wsHttpBinding> 
    </bindings> 
    <services> 
     <service behaviorConfiguration="WcfTest.Service1Behavior" name="WcfTest.TestService"> 
      <endpoint address="" binding="wsHttpBinding" contract="WcfTest.ITestService" /> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="NewBehavior" /> 
     </endpointBehaviors> 
     <serviceBehaviors> 
      <behavior name="WcfTest.Service1Behavior"> 
       <serviceMetadata httpGetEnabled="false" /> 
       <serviceDebug includeExceptionDetailInFaults="false" /> 
       <serviceCredentials> 
        <!-- Use our own custom validation --> 
        <userNameAuthentication userNamePasswordValidationMode="Custom" 
        customUserNamePasswordValidatorType="MyValidator,WcfTest"/> 
       </serviceCredentials> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
</system.serviceModel> 

和客戶端的Web.config是:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="WSHttpBinding_ITestService" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" 
        sendTimeout="00:01:00" bypassProxyOnLocal="false" 
        transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" 
        useDefaultWebProxy="true" allowCookies="false"> 
       <readerQuotas maxDepth="32" maxStringContentLength="8192" 
           maxArrayLength="16384" maxBytesPerRead="4096" 
           maxNameTableCharCount="16384" /> 
       <reliableSession ordered="true" inactivityTimeout="00:10:00" 
           enabled="false" /> 
       <security mode="Message"> 
        <transport clientCredentialType="Windows" proxyCredentialType="None" 
           realm="" /> 
        <message clientCredentialType="UserName" 
          negotiateServiceCredential="true" 
          algorithmSuite="Default" 
          establishSecurityContext="true" /> 
       </security> 
      </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:2374/Service1.svc" binding="wsHttpBinding" 
        bindingConfiguration="WSHttpBinding_ITestService" 
        contract="ServiceReference1.ITestService" 
        name="WSHttpBinding_ITestService"> 
      <identity> 
       <userPrincipalName value="NYSA31\abc" /> 
      </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

但我得到以下錯誤訪問該服務。

enter image description here

+0

您的服務與客戶之間的安全設置不匹配。您的服務正在指定消息安全性,將傳輸設置爲Basic ClientCredentialType並將消息設置爲UserName ClientCredentialType,並且客戶端正在使用消息安全性並將傳輸設置爲Windows ClientCredentialType。不知道這是否是問題,但您可能需要查看該區域。 – Tim

+0

@Tim:謝謝你的回覆。當我參考服務的默認身份驗證設置爲Windows,雖然我使用用戶名作爲身份驗證類型在我的服務。我已經對我的客戶端web.config進行了更正,但問題仍然存在。 –

+0

由於模式設置爲「消息」,因此爲傳輸的'ClientCreadentialType'配置的內容無關緊要。 –

回答

1

WsHttpBinding需求服務證書。 WCF 4(以及帶有特殊KB的舊版本)允許公開使用UserName和密碼進行身份驗證的服務而無需證書,但是您真的想要嗎?這意味着用戶名和密碼將以純文本形式通過網絡傳輸=沒有安全性,因爲任何捕獲數據包的人都可以使用被盜的憑證進行身份驗證。

要使用沒有證書的用戶名密碼,您需要custom binding或者您可以使用ClearUserNameBinding

相關問題