2014-07-21 162 views
0

我只想從使用用戶名和密碼的c#客戶端使用HTTPS-soap-webservice。 我不想使用app.config,所以我只是直接在代碼中設置了幾個屬性。HTTP請求未經授權......「匿名」

當我試圖訪問一個Web服務,方法我總是得到以下錯誤:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm......

這是我的代碼(我試過很多更認爲,但現在這是我的代碼):

var b = new WSHttpBinding(SecurityMode.TransportWithMessageCredential); 
b.Security.Message.ClientCredentialType = MessageCredentialType.UserName; 
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 

.... 
var client = new MyClient(b, new Endpoint("https://myurl...")) 
client.clientCredentials.UserName.UserName = "myuser"; 
client.clientCredentials.UserName.Password = "mypassword"; 

client.myMethodcall(); 

就像我上面寫的,我已經找到了很多「解決方案」如何解決這個錯誤,但他們只是不適合我。我使用了basicHttpBinding,我使用了WSHttpBinding,我設置了綁定超時,我使用了SecurityMode.TransportWithMessageCredential等。看起來客戶端根本沒有設置身份驗證頭!

它應該沒有區別直接在代碼中設置所有屬性或使用app.config,對不對?任何人都可以幫助我嗎?

+0

當我只使用app.config來定義它的工作沒有問題的綁定。那麼上面的代碼有什麼問題?編輯:我使用上面的EndpointAddress而不是端點。 –

回答

0

我終於找到了錯誤。

我創建的客戶端這需要在app.config的設置和我創建的客戶端針對我直接在代碼中設置的設置,發現,從在app.config所述一個使用

SecurityMode.Transport 

由默認而不是

SecurityMode.TransportWithMessageCredentials 

所以我將我的代碼更改爲SecurityMode.Transport,它的工作原理。