WCF Error {"The username is not provided. Specify username in ClientCredentials."}
Asked Answered
G

1

12

I have a WCF webservice hosted in IIS. Here is the configuration of the same:

When i try to consume this particular service (when added as a service reference) in my WinForms client, it throw this exception:

The username is not provided. Specify username in ClientCredentials.

But when i add the service as a "web reference", i can execute all the methods perfectly.

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

Code when added as a service reference:

        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

Code when added as a web reference:

        testEMService.EMSCRUD ob = new testEMService.EMSCRUD();
        ICredentials credentials = new NetworkCredential("user", "pass");
        ob.Credentials = credentials;
        ob.somemethod();//No error
Glamorize answered 22/6, 2015 at 10:6 Comment(2)
Still need help on this one...Glamorize
Do you need to include the domain? BTW why don't you submit your solution if you have resolved this problem?Shahaptian
S
31

First you can change in the config the security mode to Transport instead of TransportCredentialOnly (if there is no particular reason for it). Second, which I am sure it makes the error is that you are setting the credentials on Windows level in the code. It should be:

obj.ClientCredentials.UserName.UserName = "user";
obj.ClientCredentials.UserName.Password = "pass";
Sainted answered 17/8, 2016 at 10:48 Comment(1)
Thank you, I was tearing my hair out here. I'd set the username and password at the Windows level and knew it had to be something elementary - I'm testing without even being able to connect to the server so it had to be the client throwing the exception before it had even tried to issue the request. "Username.UserName": And I thought some of my API design was bad!Critchfield

© 2022 - 2024 — McMap. All rights reserved.