31

I am trying to call a SOAP webservice, however I am getting the error: Additional information: The username is not provided. Specify username in ClientCredentials.

So I thought I could just set client.ClientCredentials to a new instance of NetworkCredentials. However ClientCredentials is read only. So how can I go about passing this information on to access the web service?

    myService.ServiceClient client = new myService.ServiceClient();
    // This won't work since its read only.                
    client.ClientCredentials = new System.Net.NetworkCredential("username", "password", "domain");
    string version = client.getData();

EDIT: Binding:

  <binding name="VersionHttpBinding">
    <security mode="TransportCredentialOnly">
      <transport clientCredentialType="Basic" />
    </security>
  </binding>
John Doe
  • 3,053
  • 17
  • 48
  • 75

1 Answers1

60

You'll need to set the credentials on the client, like as shown in this MSDN article:

client.ClientCredentials.UserName.UserName = "my_user_name";
client.ClientCredentials.UserName.Password = "my_password";
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Martin Costello
  • 9,672
  • 5
  • 60
  • 72
  • 1
    I'm 99% sure I've done exactly this in the past. Did you try and replace the entire object (which I believe *is* read-only), or setting the individual properties on the object, as in the example. – Martin Costello May 09 '16 at 16:45
  • 55
    .Username.Password. Hilarious – Gaspa79 Feb 19 '18 at 16:52
  • Is providing passwords in this way secure or it requires SSL to be implemented as well? – bjan Jun 12 '18 at 06:22
  • 1
    You should always use HTTPS and TLS if exchanging credentials. I'm not sure as I can't remember and haven't used WCF for some time, but it might even be a requirement of the WCF APIs that you must use HTTPS with username and password. – Martin Costello Jun 12 '18 at 07:26
  • @Martin Costello: It is no requirement. Your solution, combined with an app.config that is configured the way John Doe shows it, does work. – Christoph Jan 31 '20 at 17:42
  • I was giving up when I saw "Username" {get} – Özgür Ağcakaya Oct 21 '21 at 05:44