我写了一个Wcf服务,其配置文件是
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="securityDemo">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfDemo.EmpService" behaviorConfiguration="MyServiceTypeBehaviors">
<host>
<baseAddresses>
<add baseAddress="http://localhost:3003/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WcfDemo.IEmpService" bindingConfiguration="securityDemo"/>
</service>
</services>
</system.serviceModel>
这适用于控制台应用或WPF应用。 但是当我在Windows 8.1应用程序中使用相同的服务时,我得到了这个异常
HTTP请求未经授权使用客户端身份验证方案 '谈判'。从服务器收到的身份验证标头是 '协商,NTLM'。
答案 0 :(得分:1)
在使用服务代理之前添加带标识的行:
var client = new ServiceReference1.Service1Client();
client.ClientCredentials.Windows.ClientCredential.UserName =&#34; windows user name&#34 ;;
client.ClientCredentials.Windows.ClientCredential.Password =&#34; user passw&#34;;
调用服务方法