【发布时间】:2015-07-27 12:27:42
【问题描述】:
我是 WCF/API 的新手,对安全性知之甚少。如果我需要提供更多信息,请告诉我。
我正在尝试使用
连接到服务<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISalesOrderService">
<security mode="Transport" >
<transport clientCredentialType="Basic"></transport>
</security>
</binding>
<binding name="BasicHttpBinding_IDocumentationService">
<security mode="TransportCredentialOnly" >
<transport clientCredentialType="Basic"></transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="address1"
name="BasicHttpBinding_ISalesOrderService"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ISalesOrderService"
contract="SoCalls.ISalesOrderService" />
<endpoint address="address2"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IDocumentationService"
contract="DocCalls.IDocumentationService"
name="BasicHttpBinding_IDocumentationService" />
</client>
</system.serviceModel>
有了这个,我得到这个错误:
'System.ServiceModel.Security.MessageSecurityException'
The HTTP request is unauthorized with client authentication scheme 'Basic'.
The authentication header received from the server was 'Basic Realm'.
编辑 我按照 cmets 中提供的链接中建议的说明进行操作,仍然给我这个错误。我更新了我的代码,但由于缺乏对这两种服务的了解,我对是否使用 HTTP/HTTPS 仍然有些困惑。
这是我实例化服务的方式:
private static SoCalls.SalesOrderServiceClient CreateSalesOrderServiceClient()
{
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.MaxReceivedMessageSize = 10000 * 2;
myBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
EndpointAddress ea = new EndpointAddress("address1");
SoCalls.SalesOrderServiceClient client = new SoCalls.SalesOrderServiceClient();
client.ClientCredentials.UserName.UserName = ("username");
client.ClientCredentials.UserName.Password = ("password");
return client;
}
【问题讨论】:
-
@CodeCaster 重复,你的意思是要声明两个
吗?我知道地址不同,因为它们是我项目中的两个不同的服务引用。你认为我设置它们的方式会在运行时混淆程序吗? - 谢谢! -
该评论意味着您的问题似乎与问题"Calling a web service that requires basic http authentication from wcf client" 重复,后者是一个链接。点击它,实施建议的解决方案。
-
@CodeCaster 啊,对不起。感谢您的链接。
-
展示如何实例化客户端代理以及如何设置身份验证信息。
标签: wcf authentication wcf-security