web-services - WCF 错误 {"The username is not provided. Specify username in ClientCredentials."}

标签 web-services wcf web service-reference web-reference

我有一个 WCF web 服务托管在 IIS 中。这是相同的配置:

当我尝试在我的 WinForms 客户端中使用此特定服务(作为服务引用添加时)时,它会抛出此异常:

The username is not provided. Specify username in ClientCredentials.



但是当我将服务添加为“网络引用”时,我可以完美地执行所有方法。
<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpEndpointBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

      <services>
        <service behaviorConfiguration="EMSServiceBehavior"
                               name="EMSService.EMSCRUD">
          <endpoint address="" binding="basicHttpBinding"
          bindingConfiguration="BasicHttpEndpointBinding"
          name="BasicHttpEndpoint" contract="EMSService.IEMSCRUD">
          </endpoint>
        </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="EMSServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
          <!--<serviceCredentials>
            <windowsAuthentication allowAnonymousLogons="False" includeWindowsGroups="True"/>
          </serviceCredentials>-->
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

添加为服务引用时的代码:
        EE.EMSCRUDClient obj = new EE.EMSCRUDClient();
        obj.ClientCredentials.Windows.ClientCredential.UserName = "demo";
        obj.ClientCredentials.Windows.ClientCredential.Password = "demo";
        obj.ClientCredentials.Windows.ClientCredential.Domain = "demo";
        obj.somemethod(); //Error here

添加为 Web 引用时的代码:
        testEMService.EMSCRUD ob = new testEMService.EMSCRUD();
        ICredentials credentials = new NetworkCredential("user", "pass");
        ob.Credentials = credentials;
        ob.somemethod();//No error

最佳答案

首先,您可以在配置中将安全模式更改为 Transport 而不是 TransportCredentialOnly(如果没有特殊原因)。
其次,我确信它会导致错误是您在代码中设置 Windows 级别的凭据。它应该是:

obj.ClientCredentials.UserName.UserName = "user";
obj.ClientCredentials.UserName.Password = "pass";

关于web-services - WCF 错误 {"The username is not provided. Specify username in ClientCredentials."},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30977202/

相关文章:

java - 通过 JSP 使用 Web 服务 JAX

java - 尝试通过 php webservice 发送数组时出现异常

java - 通过 JAX-WS 和 WS-Addressing 使用 SOAP

wcf - 在 IIS7 上部署 WCF 教程应用程序 : "The type could not be found"

javascript - 将文件上传到网站,调用Python脚本并以最轻量的方式下载生成的新文件

java - 如何在 Jersey Web 服务中保留类变量的值

c# - WCF 和身份验证

jquery - 在不同项目中通过 AJAX 调用使用 jQuery 中的 WCF 服务(跨域)

audio - 您会为服务器端合成器推荐什么设置?

android - 移动应用程序的后端服务器