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

标签 c# winforms web-services

我在 Windows 应用程序中使用 WSDL 使用 Web 服务。当我尝试使用方法时,出现以下错误:-

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was '"

{"The remote server returned an error: (401) Unauthorized."}

我有用户凭据,但不知道如何在 Windows 应用程序中使用 C# 代码传递它。

最佳答案

这是它对我的工作方式:-

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

<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>

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

 var ser = new GetDataClient();
 ser.ClientCredentials.UserName.UserName = "userid";
 ser.ClientCredentials.UserName.Password = "Pa$$word1";

关于c# - 如何将用户凭据传递给 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17031999/

相关文章:

c# - MySQL 连接器网络 6.9.9 设置向导提前结束

c# - 在 LINQ 中有条件地加入

c# - 如何在 DateTimePicker 控件中自定义日历?

xml - 是否有我可以查询以获取 MLS 数据的免费/付费网络服务?

javascript - 关于将大字符串发布到 Web 服务的建议

c# - 我可以使用 LINQ 从 List<String> 返回 List<Person>

c# - 除了可选参数之外,两个方法具有相同的签名

c# - 如何在 C# Windows 应用程序的数据 GridView 中添加超过 65,535 列?

c# - .NET Framework 在更改特定属性之前是否执行内部检查?

web-services - 使用Delphi消费oData服务建议