c# - (WCF) 调用者未通过服务的身份验证
问题描述
我已经创建了 WCF 服务 + 客户端并将服务部署到另一台 PC。服务正在运行。但是,当我通过 Visual Studio 在调试模式下执行客户端时,我收到错误消息:调用者未通过服务进行身份验证。PC、客户端和服务器都在同一个本地网络上。
服务器端(服务)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<!-- This section is optional with the new configuration model
introduced in .NET Framework 4. -->
<service name="Digiteq.Services.LabSat.LabSatService"
behaviorConfiguration="LabSatServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/LabSat3/service"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8733/LabSat/service -->
<endpoint address=""
binding="wsHttpBinding"
contract="Digiteq.Services.LabSat.ILabSat" />
<!-- the mex endpoint is exposed at http://localhost:8733/LabSat3/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="LabSatServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
客户端配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ILabSat" 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="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.228.65:8000/LabSat3/service" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ILabSat" contract="ILabSat"
name="WSHttpBinding_ILabSat">
<identity>
<servicePrincipalName value="host/MBO-NEW" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
解决方案
我认为问题可能是由客户端凭据引起的。据我所知,wsHttpBinding传输安全模式默认为Message,而我们设置传输安全为message时,客户端凭证类型默认为windows。所以我们需要明确提供可以被服务器端认证的凭证。
client.ClientCredentials.Windows.ClientCredential.UserName = "username";
client.ClientCredentials.Windows.ClientCredential.Password = "password";
这里是官方文档。 官方文件
也有可能是服务端需要标识,然后客户端需要手动指定endpointidentity。这取决于您的配置和托管环境。 官方文件
推荐阅读
- ios - Swift:textField 侦听器不响应文本更改
- python - Linux 中的 Pandas 失败,Windows 中未发生 - 缺少 _data 属性
- javascript - 将值返回到另一个嵌套函数中的函数
- angular - 在 apache 托管服务器上运行 angular9 应用程序
- google-apps-script - 如何使用谷歌应用脚本从另一张表中选择数值?
- mysql - MYSQL 使用触发器使用更新行中的值更新另一行
- python - 最大的小日期
- azure-devops - Azure devops Rest API 调用版本
- python-3.x - 使用字典实例与列表实例重复代码
- scala - 在 spark-avro 中找不到模式注册表的 to_avro 函数