C# 如何设置 ClientCredentials?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12509992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set ClientCredentials?
提问by user1428798
I'm trying to consume a WCF service:
我正在尝试使用 WCF 服务:
The config of the service is:
服务的配置是:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<netNamedPipeBinding>
<binding name="netNamedPipeEndpoint" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288"
maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport protectionLevel="EncryptAndSign" />
</security>
</binding>
</netNamedPipeBinding>
<netTcpBinding>
<binding name="netTcpEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
<wsHttpBinding>
<binding name="wsHttpBindingConfiguration" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="net.tcp://webapppro-v11/FlightInfoWebService/FlightInfoService.svc"
binding="netTcpBinding" bindingConfiguration="netTcpEndpoint"
contract="FlightInfoService" name="netTcpEndpoint" />
<endpoint address="net.pipe://webapppro-v11/FlightInfoWebService/FlightInfoService.svc"
binding="netNamedPipeBinding" bindingConfiguration="netNamedPipeEndpoint"
contract="FlightInfoService" name="netNamedPipeEndpoint" />
<endpoint address="http://webapppro-v11/FlightInfoWebService/FlightInfoService.svc"
binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration"
contract="FlightInfoService" name="wsHttpBindingConfiguration" />
</client>
</system.serviceModel>
</configuration>
When I try to call the service:
当我尝试调用服务时:
public async void LoadCities()
{
_client = new FlightInfoServiceClient(Maquette_MyAirport_Win8.FlightService.FlightInfoServiceClient.EndpointConfiguration.wsHttpBindingConfiguration, "http://servuucs.fr/FlightInfoWebService/FlightInfoService.svc");
var citiesResponse = await _client.GetAllCitiesAsync(new BaseRequest());
var myCities = citiesResponse.Cities;
}
I catch this exception:
我抓住了这个异常:
ERROR: UnAuthorizedAccess description:You are not authorized to access this service
错误:UnAuthorizedAccess 说明:您无权访问此服务
How can I set my ClientCredentials?
如何设置我的 ClientCredentials?
采纳答案by Jeroen
As @Paciv noted in a comment, you can do this through code. Set them with the property ClientCredentials.Windows, something like this:
正如@Paciv 在评论中指出的那样,您可以通过代码来做到这一点。使用属性ClientCredentials.Windows设置它们,如下所示:
_client.ClientCredentials.Windows.ClientCredential.Domain = "warzone42";
_client.ClientCredentials.Windows.ClientCredential.UserName = "user1428798";
_client.ClientCredentials.Windows.ClientCredential.Password = "p@ssw0rd";
Setting the credentials in code is of course unwise. If you don't set the Windows user programmatically as above, I believe the credentials from the user running the client are sent accross (which is perhaps a more typical situation?).
在代码中设置凭据当然是不明智的。如果您没有像上面那样以编程方式设置 Windows 用户,我相信来自运行客户端的用户的凭据是交叉发送的(这可能是更典型的情况?)。
Note that if you're setting credentials in code you may in fact be looking for UserName authentication.
请注意,如果您在代码中设置凭据,您实际上可能正在寻找UserName authentication。

