【问题标题】:How can I set ClientCredentials?如何设置 ClientCredentials?
【发布时间】:2012-09-20 09:43:50
【问题描述】:

我正在尝试使用 WCF 服务:

服务的配置是:

<?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>

当我尝试调用服务时:

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;
}

我发现了这个异常:

错误:UnAuthorizedAccess 描述:您无权访问此服务

如何设置我的 ClientCredentials?

【问题讨论】:

  • 请发布您的整个&lt;system.servicemodel/&gt; 配置。也请格式化它以便阅读。不要依赖社区来修复您的格式。谢谢
  • 我更新了配置,请问有什么建议吗?
  • 标题和问题描述不同

标签: c# wcf credentials


【解决方案1】:

正如@Paciv 在评论中指出的那样,您可以通过代码执行此操作。使用属性ClientCredentials.Windows 设置它们,如下所示:

_client.ClientCredentials.Windows.ClientCredential.Domain = "warzone42";
_client.ClientCredentials.Windows.ClientCredential.UserName = "user1428798";
_client.ClientCredentials.Windows.ClientCredential.Password = "p@ssw0rd";

在代码中设置凭据当然是不明智的。如果您没有像上面那样以编程方式设置 Windows 用户,我相信来自运行客户端的用户的凭据会被发送过来(这可能是更典型的情况?)。

请注意,如果您在代码中设置凭据,您实际上可能正在寻找UserName authentication

【讨论】:

  • 可能是。但它回答了您的问题(“如何设置客户端凭据?”)。
  • @lordkain(除此之外我的回答是 4 岁-)我不确定你的意思。我的答案中的这些示例凭据是否安全?哎呀,不。是 SecureString 条目吗?没有。在代码中硬编码凭据是否安全?可能不是。通常使用代码设置凭据是否安全?我当然希望如此:D。使用 Windows.ClientCredential 是否足够安全?这取决于您的要求和背景;我们无法为您解答。
  • @Jeroen。 4 年 :) 我只是想知道安全方面的替代方案。这里不是讨论的地方。
  • 在服务clientCredentialType 设置为Windows 时有效。注意,您可以使用其他方式设置usernamepassword(如_client.ClientCredentials.UserName
猜你喜欢
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多