我正在使用ASMX Web服务,该服务使用需要“ UserName”和“ Password”的基本身份验证。 我一直试图在Visual Studio中将服务添加为“服务引用”。 我收到以下异常:
HTTP请求未经客户端身份验证方案授权 “基本”。从服务器收到的身份验证标头为“基本” realm =“ NTADMIN”
但是,当我将该服务添加为Web Reference时,身份验证开始起作用并且得到响应。
我想通过将服务添加为“服务参考”来运行代码。
代码不起作用(服务参考):
WOVServicesSoapClient client = new WOVServicesSoapClient();
client.ClientCredentials.UserName.UserName = "UserNameHere";
client.ClientCredentials.UserName.Password = "PasswordHere";
var response = client.GetDocuments(documentInput);
App.config用于不起作用的代码(服务参考)::
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="IWOVServicesSoap">
<security mode="Transport">
<transport clientCredentialType="Basic" realm="NTADMIN"> </transport>
</security>
</binding>
<binding name="IWOVServicesSoap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https:/URLPART/WorkSite/services/IWOVServices.asmx"
binding="basicHttpBinding" bindingConfiguration="IWOVServicesSoap"
contract="WorkSite.IWOVServicesSoap" name="IWOVServicesSoap" />
</client>
</system.serviceModel>
</configuration>
工作代码(网络参考):
IWOVServices obj = new IWOVServices();
obj.Credentials = new NetworkCredential("UserNameHere", "PasswordHere", "NTADMIN");
var response = obj.GetDocuments(documentInput);