The HTTP request is unauthorized with client authentication scheme ‘Anonymous’. The authentication header received from the server was ‘Negotiate,NTLM
参考文章:
(重要)https://gist.github.com/Krizzzn/3127827
(辅助)http://stackoverflow.com/questions/13704720/the-http-request-is-unauthorized-with-client-authentication-scheme-ntlm-the-a
(辅助)https://msdn.microsoft.com/en-us/library/ms733836.aspx
修改 app.config,增加红色字体部分。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="PROVIDERSSoapBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://xxx.xx.xx.xxx:9011/provider/services/PROVIDERS"
binding="basicHttpBinding" bindingConfiguration="PROVIDERSSoapBinding"
contract="ServiceReference1.ProviderRemote" name="PROVIDERS" />
</client>
</system.serviceModel>
将认证模式代码加入:红色字体部分很重要,必须。
var sms = new SMS.ServiceReferenceSMS.SMSSoapClient();
sms.Endpoint.Address = new System.ServiceModel.EndpointAddress(“http://xxx/WebService.asmx”);
if (sms.ClientCredentials != null)
{
sms.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
sms.ClientCredentials.Windows.AllowNtlm = true;
sms.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(“username”, “password”, “domain”);
}
sms.Open();