通过channelfactory调用WCF webservice时出错 - 未经许可

时间:2016-09-07 15:37:16

标签: c# wcf authentication

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

  

HTTP请求未经授权使用客户端身份验证方案   '匿名&#39 ;.从服务器收到的身份验证标头是   '协商,NTLM'

我的通话代码

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

我的WCF服务web.config system.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身份验证来传递代码运行的帐户(服务帐户),但它似乎是匿名发送

1 个答案:

答案 0 :(得分:0)

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

var myBinding = new BasicHttpBinding(SecurityMode.TransportCredentialOnly);

作为替代方法,您可以在绑定的构造函数中设置模式:

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

同时设置ClientCredential:

{{1}}