我有一些使用WCF服務的代碼。該服務由基本身份驗證保護,所以在創建客戶端,我用下面的代碼:當我運行從一個控制檯應用程序的代碼從Windows服務調用WCF安全異常
BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
httpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
httpBinding.Security.Transport.Realm = service_realm;
EndpointAddress address = new EndpointAddress(service_address);
Service.ServiceClient client = new Service.ServiceClient(httpBinding, address);
client.ClientCredentials.UserName.UserName = service_username;
client.ClientCredentials.UserName.Password = service_password;
工作正常。但是當我從Windows服務運行相同的代碼時,正在拋出MessageSecurityException,告訴我我的請求未經授權。出於某種原因,它似乎使用當前的Windows帳戶進行身份驗證,因爲我自己的帳戶有權訪問該服務。但我不希望它,我希望它使用存儲的憑據。我在這裏錯過了什麼?
這可能是這樣,但這用於當我有一個正常的app.config時工作。改變的是我現在通過編程方式創建了綁定,這似乎是導致問題的原因。我認爲你的聲明對於wsHttpBinding是正確的,但不是basicHttpBinding。 – Jasper