Web services WCF错误{“未提供用户名。请在ClientCredentials中指定用户名。”

Web services WCF错误{“未提供用户名。请在ClientCredentials中指定用户名。”,web-services,wcf,web,service-reference,web-reference,Web Services,Wcf,Web,Service Reference,Web Reference,我在IIS中托管了一个WCF Web服务。以下是相同的配置: 当我尝试在WinForms客户端中使用此特定服务(添加为服务引用时),它会引发以下异常: 未提供用户名。在ClientCredentials中指定用户名 但是,当我将服务添加为“web引用”时,我可以完美地执行所有方法 <system.serviceModel> <bindings> <basicHttpBinding> <binding name="Bas

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

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

未提供用户名。在ClientCredentials中指定用户名

但是,当我将服务添加为“web引用”时,我可以完美地执行所有方法

<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>
添加为web引用时的代码:

        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

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

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

还需要这方面的帮助…你需要包括域名吗?顺便说一句,如果你已经解决了这个问题,为什么不提交你的解决方案呢?谢谢你,我在这里把我的头发扯得乱七八糟。我在Windows级别设置了用户名和密码,并且知道它必须是基本的东西——我正在测试,甚至无法连接到服务器,因此它必须是在试图发出请求之前抛出异常的客户端。“Username.Username”:我认为我的一些API设计很糟糕!