【发布时间】:2014-11-24 09:37:04
【问题描述】:
我正在尝试将 endpoint 和 wshttpbinding 配置移动到 c# 文件,以便我可以在运行时更改端点地址(从下拉列表、进程等中选择)。但是在创建WsHttpBinding 对象后,EndpointAddress 对象并将它们传递给我的客户。它会抛出以下异常:
System.ServiceModel.FaultException:调用者未通过身份验证 通过服务
如果用户凭据不正确,我会遇到同样的异常。但是,它们并没有从使用 Web.config 文件更改为以编程方式创建这些配置选项。
Web.config(有效):
<binding name="myService" maxReceivedMessageSize="2147483647">
<readerQuotas
maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" establishSecurityContext="false" />
</security>
</binding>
<client>
<endpoint
address="https://address/myservice.svc"
binding="wsHttpBinding"
bindingConfiguration="myService"
contract="MyService.IMyService"
name="myService"
/>
</client>
MyService service = new MyService();
service.username = "user";
service.password = "pass";
//Success
以编程方式(不起作用):
WSHttpBinding wsHttp = new WSHttpBinding();
wsHttp.MaxReceivedMessageSize = 2147483647;
wsHttp.ReaderQuotas.MaxDepth = 2147483647;
wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
wsHttp.ReaderQuotas.MaxArrayLength = 2147483647;
wsHttp.ReaderQuotas.MaxBytesPerRead = 2147483647;
wsHttp.ReaderQuotas.MaxNameTableCharCount = 2147483647;
wsHttp.Security.Mode = SecurityMode.TransportWithMessageCredential;
wsHttp.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
wsHttp.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
wsHttp.Security.Message.EstablishSecurityContext = false;
EndpointAddress endpoint = new EndpointAddress("https://address/myservice.svc");
MyService service = new MyService(wsHttp, endpoint);
service.username = "user";
service.password = "pass";
//System.ServiceModel.FaultException: The caller was not authenticated by the service
我已尝试遵循教程/查看答案,但我无法弄清楚。
【问题讨论】:
-
您在哪一行遇到错误。
-
@AshokRathod 每当我从服务调用任何方法时都会收到错误消息。
service.myMethod()