2011-11-22 340 views
18

在簡單的測試Wcf服務中使用基本身份驗證遇到一些麻煩。我收到一個例外:Wcf基本身份驗證

無法激活請求的服務'http://qld-tgower/test/Service.svc'。請參閱>服務器的診斷跟蹤日誌以獲取更多信息。

而在跟蹤日誌它顯示:

主機(「基本」)上配置的身份驗證方案不允許那些綁定「basicHttpBinding的」(「匿名」)進行配置。請確保SecurityMode設置爲Transport或TransportCredentialOnly。此外,可以通過更改此應用程序的身份驗證方案,通過IIS管理工具,通過ServiceHost.Authentication.AuthenticationSchemes屬性,<serviceAuthenticationManager>元素上的應用程序配置文件,通過更新綁定上的ClientCredentialType屬性,或者通過調整HttpTransportBindingElement上的AuthenticationScheme屬性。

但當我我們不正確的用戶名和密碼,它使用基本身份驗證說它IS什麼我不明白呢?

該HTTP請求未經授權,客戶端身份驗證方案爲「基本」。從服務器收到的驗證頭是'Basic realm =「qld-tgower」'。

這是我的web.config細節

<system.serviceModel> 
<services> 
    <service name="WcfService" 
     behaviorConfiguration="Behavior"> 
    <endpoint address="http://QLD-TGOWER/test/Service.svc" 
       binding="basicHttpBinding" 
       bindingConfiguration="httpBinding" 
       contract="IService" /> 
    </service> 
</services> 
<diagnostics> 
    <endToEndTracing activityTracing="false" messageFlowTracing="true" propagateActivity="true"></endToEndTracing> 
</diagnostics> 
<bindings> 
    <basicHttpBinding> 
    <binding name="httpBinding"> 
     <security mode="TransportCredentialOnly"> 
     <transport clientCredentialType="Basic" proxyCredentialType="Basic"> 
     </transport> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 
<behaviors> 
    <serviceBehaviors> 
    <behavior> 
     <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
     <serviceMetadata httpGetEnabled="true"/> 
     <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
     <serviceDebug includeExceptionDetailInFaults="true"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> 
</system.serviceModel> 

,這是我的App.config

<system.serviceModel> 
    <diagnostics> 
     <endToEndTracing activityTracing="true" /> 
     <messageLogging logMessagesAtTransportLevel="true" /> 
    </diagnostics> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="BasicHttpBinding_IService" > 
      <security mode="TransportCredentialOnly"> 

      <transport clientCredentialType="Basic" proxyCredentialType="Basic"></transport> 
      <message clientCredentialType="UserName" /> 
      </security> 

     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://QLD-TGOWER/test/Service.svc" binding="basicHttpBinding" 
     bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService" 
     name="BasicHttpBinding_IService" /> 
    </client> 
</system.serviceModel> 

我的測試應用程序

private static void Main(string[] args) 
{ 
    var proxy = new ServiceClient("BasicHttpBinding_IService"); 
    var clientCredentials = proxy.ClientCredentials; 
    clientCredentials.UserName.UserName = "username"; 
    clientCredentials.UserName.Password = "password"; 
    var res = proxy.GetData(1); 
    Console.WriteLine(res); 
    Console.WriteLine("Done"); 
    Console.ReadKey(true); 
} 

我的服務

public class Service : IService 
{ 

    public string GetData(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 
} 

有沒有,我很想念這裏的東西嗎?

回答

17

更改服務的名稱和合約以包含名稱空間。

另外,刪除端點地址(將其設置爲「」),並且不要在transport標籤中包含proxyCredentialType。

web.config中的最終結果應該是這個樣子

<system.serviceModel> 

    <services> 
     <service name="MyNameSpace.MyService" behaviorConfiguration="asdf"> 
     <endpoint address="" binding="basicHttpBinding" 
      bindingConfiguration="httpBinding" contract="MyNameSpace.IMyService" /> 
     </service> 
    </services> 

    <diagnostics> 
     <endToEndTracing activityTracing="true" messageFlowTracing="true" 
      propagateActivity="true"> 
     </endToEndTracing> 
    </diagnostics> 

    <bindings> 
     <basicHttpBinding> 
     <binding name="httpBinding"> 
      <security mode="TransportCredentialOnly"> 
      <transport clientCredentialType="Basic" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="asdf"> 
      <!-- To avoid disclosing metadata information, set the value below to 
       false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true" /> 
      <!-- To receive exception details in faults for debugging purposes, 
       set the value below to true. Set to false before deployment to avoid 
       disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 

     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <serviceHostingEnvironment multipleSiteBindingsEnabled="false"/> 

    </system.serviceModel> 
2

爭取客戶端和服務器CONFIGS

<basicHttpBinding> 
    <binding name="BasicHttpBinding_IService"> 
     <security mode="TransportCredentialOnly"> 
      <transport clientCredentialType="Basic" /> 
     </security> 
    </binding> 
</basicHttpBinding> 

安裝/啓用基本身份驗證

您可能還需要安裝和IIS應用基本身份驗證。

轉到「程序和功能」/「打開/關閉窗口功能」。 在IIS和安全性的某個位置啓用「基本身份驗證」。

我關閉並打開了IIS控制檯,並且能夠在身份驗證設置下啓用它。

這當然,如果對於開發測試,它會警告你沒有SSL證書。

+0

謝謝,這已經修復了認證錯誤,但是現在該服務進入了故障狀態。跟蹤日誌中沒有任何內容可以說明原因。 – TheRealTy

1

不允許你通過不安全的連接使用的用戶名認證

您可以通過我用ClearUsernameBinding安全傳輸(例如SSL)或消息加密(使用證書)

確保消息在過去取得巨大成功,但我不推薦它在生產中。我使用它,以便我可以保持所有身份驗證代碼不變,而不需要在開發/測試環境中使用SSL,但只需更改配置即可使用SSL。

注意:該自定義綁定並不完美,我必須稍微更改它以啓用某些配置更改。

0

這就是解決了我的問題:

<bindings> 
    <basicHttpBinding> 
    <binding> 
     <security mode="TransportCredentialOnly"> 
     <transport clientCredentialType="Windows" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

更多信息參見: https://msdn.microsoft.com/en-gb/library/ff648505.aspx