32

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>
2
  • What is the authentication method of the web service? Basic authentication or something else?
    – mikey
    Commented May 9, 2016 at 16:31
  • Updated as requested.
    – John Doe
    Commented May 9, 2016 at 16:33

1 Answer 1

64

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";
6
  • 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. Commented May 9, 2016 at 16:45
  • 57
    .Username.Password. Hilarious
    – Gaspa79
    Commented Feb 19, 2018 at 16:52
  • Is providing passwords in this way secure or it requires SSL to be implemented as well?
    – bjan
    Commented Jun 12, 2018 at 6: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. Commented Jun 12, 2018 at 7: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
    Commented Jan 31, 2020 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.