我有一些使用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帐户进行身份验证,因为我自己的帐户确实可以访问该服务。但我不希望它,我希望它使用存储的凭据。我在这里错过了什么?
答案 0 :(得分:0)
WCF basicHttpBinding 不支持纯文本凭据;原因是因为您希望在传输绑定上传递凭据的那一刻,WCF要求底层传输是安全传输,例如SSL。
为了让您的代码正常工作,您需要通过https
或使用证书或加密来使用服务。
答案 1 :(得分:0)
似乎使用此配置修复:
_httpBinding = new BasicHttpBinding();
_httpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; _httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; _httpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
_httpBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName; _httpBinding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
_httpBinding.AllowCookies = false; _httpBinding.BypassProxyOnLocal = false; _httpBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; _httpBinding.MessageEncoding = WSMessageEncoding.Text; _httpBinding.TextEncoding = Encoding.UTF8; _httpBinding.TransferMode = TransferMode.Buffered; _httpBinding.UseDefaultWebProxy = false; Service.ServiceClient client = new Service.ServiceClient(_httpBinding,_address);
client.ClientCredentials.UserName.UserName = service_username; client.ClientCredentials.UserName.Password = service_password;