如何将用户凭据传递给Web服务?

use*_*064 12 c# web-services winforms

我正在使用Windows应用程序中的WSDL来使用Web服务.当我尝试使用方法时,我收到以下错误: -

HTTP请求未经授权,客户端身份验证方案为"匿名".从服务器收到的身份验证标头是""

{"远程服务器返回错误:(401)未经授权."}

我有用户凭据但不知道如何使用Windows应用程序中的c#代码传递它.

use*_*064 12

以下是它如何为我工作: -

配置文件设置如下所示: -

<configuration>
    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="bindingName"  >
              <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Basic" proxyCredentialType="None" realm=""/>
                <message clientCredentialType="UserName" algorithmSuite="Default"/>
              </security>
            </binding>

          </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://10.10.10.10:1880/testpad/services/Testwebservice"
                binding="basicHttpBinding" bindingConfiguration="bindingName"
                contract=testService.GetData" name="test_Port1" />
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

在这里,我传递用户凭据: -

 var ser = new GetDataClient();
 ser.ClientCredentials.UserName.UserName = "userid";
 ser.ClientCredentials.UserName.Password = "Pa$$word1";
Run Code Online (Sandbox Code Playgroud)