通过channelfactory调用WCF web服务出错-不被允许

本文关键字:出错 服务 channelfactory 调用 WCF web 通过 | 更新日期: 2023-09-27 18:06:15

我正在使用ChannelFactory调用WCF服务(因为目标服务位置将根据环境而更改,并且我需要URL是可配置的)。但是我得到了错误:

使用客户端认证方案,HTTP请求未授权"匿名"。日志含义从服务器接收到的认证头为"谈判,NTLM"。

我的呼叫码

var myBinding = new BasicHttpBinding();
var myEndpoint = new EndpointAddress(webserviceAddress);
var myChannelFactory = new ChannelFactory<IObjectService>(myBinding, myEndpoint);
var serviceClient = myChannelFactory.CreateChannel();

我的WCF服务web。配置系统。servicemodel节

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding>
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
  </system.serviceModel>

服务应该基于Windows身份验证进行身份验证。我本以为默认情况下,上面的调用代码将使用Windows身份验证来传递该代码作为(服务帐户)运行的帐户,但它似乎正在发送匿名

通过channelfactory调用WCF web服务出错-不被允许

必须将模式设置为使用消息凭据进行传输,如下面的代码所示:

var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = SecurityMode.TransportCredentialOnly;
作为一种选择,您可以在绑定的构造函数中设置模式:
var myBinding = new BasicHttpBinding(SecurityMode.TransportCredentialOnly);

也设置ClientCredential:

myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;