【问题标题】:WCF Error {"The username is not provided. Specify username in ClientCredentials."}WCF 错误 {“未提供用户名。在 ClientCredentials 中指定用户名。”}
【发布时间】:2015-06-22 10:06:57
【问题描述】:

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

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

未提供用户名。在 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

添加为网络参考时的代码:

        testEMService.EMSCRUD ob = new testEMService.EMSCRUD();
        ICredentials credentials = new NetworkCredential("user", "pass");
        ob.Credentials = credentials;
        ob.somemethod();//No error

【问题讨论】:

  • 在这方面仍然需要帮助...
  • 是否需要包含域?顺便说一句,如果您解决了这个问题,为什么不提交您的解决方案?

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


【解决方案1】:

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

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

【讨论】:

  • 谢谢,我把头发扯掉了。我在 Windows 级别设置了用户名和密码,并且知道它必须是基本的 - 我正在测试,甚至无法连接到服务器,所以它必须是客户端在它甚至尝试之前抛出异常发出请求。 “Username.UserName”:而且我认为我的一些 API 设计很糟糕!
猜你喜欢
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2021-10-05
相关资源
最近更新 更多