2012-09-24 127 views
2

在我的一個應用程序中,我通過Windows帳戶連接和驗證WCF服務時遇到問題。爲了測試這一點,我已經將它轉變爲一個簡單的控制檯應用程序和VS2010中的標準WCF啓動應用程序的新解決方案。WCF Windows身份驗證,未通過

WCF代碼,一個簡單的動作:

[PrincipalPermission(SecurityAction.Demand, Role = "xxxx\\xxxxxx")] 
    public string GetData(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 

配置文件:

<bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_IService1"> 
      <security mode="TransportCredentialOnly"> 
       <transport clientCredentialType="Windows" proxyCredentialType="Windows"/> 
       <message clientCredentialType="UserName" algorithmSuite="Default"/> 
      </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:16674/Service1.svc" binding="basicHttpBinding" 
      bindingConfiguration="BasicHttpBinding_IService1" contract="testService.IService1" 
      name="BasicHttpBinding_IService1" /> 
    </client> 

和呼叫這樣:

testService.Service1Client sv = new testService.Service1Client(); 
sv.GetData(1); 

我得到的標準「請求主體許可失敗。「錯誤,雖然我看不到配置文件是錯誤的。在創建服務對象時,我查看了服務對象,並且ClientCredentials.Windows和username對象爲空。任何人都可以幫助我,我在這裏是愚蠢的嗎?

感謝

回答

0

您需要使用代碼來設置憑據,請參閱本article

testService.Service1Client sv = new testService.Service1Client(); 
sv.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultCredentials; 
sv.GetData(1); 
+2

我認爲那篇文章可能會過時,但我發現下面 sv.ClientCredentials.Windows .ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials; 但DefaultNetworkCredentials對象是空的? – nik0lias

+0

我更新了代碼,您是否嘗試過運行它?你仍然得到認證錯誤?如果我沒有記錯信用只看起來空... –

+0

看到這個鏈接重新空憑證http://stackoverflow.com/a/7805911/1045728 –