How Do I Pass Credentials to a Web Service that Uses Basic Authentication?

When you're working with Web Services that require authentication, you'll need a way to pass credentials. This snippet shows you how to pass a user name and password to a Web Service that is configured for Basic Authentication.
using System.Net;
localhost.MyWebService myService = new localhost.MyWebService();
System.Net.CredentialCache myCredentials = new System.Net.CredentialCache(); NetworkCredential netCred = new NetworkCredential("UserName", "Password"); myCredentials.Add(new Uri(myService.Url), "Basic", netCred); myService.Credentials = myCredentials; // Now call methods on the myService object....

You should be aware that by using Basic Authentication the user name and password are sent as Base 64 encoded text. That is as secure as sending it as plain text, so the best way to use this authentication mechanism is in combination with SSL. This way, the data is encrypted and is save from prying eyes....

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .

Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.

Feedback by Other Visitors of Imar.Spaanjaars.Com

On Saturday, April 8, 2006 8:58:36 AM dutguoyi said:
That is what I wanted.
Thank you.
Security in ASP.NET1.1 is my favorite technique.
I need information about cross-site in ASP.NET1.1.
Thank you very much.

On Monday, January 15, 2007 2:21:41 AM surekha said:
i want to know how to upload photos or images into asp.net1.1. As i am working in visual studio 2003 version . so i have to use html file upload button there. after using that how to upload photo there. pleas help me out . i want to add the photos of students even into database .
On Monday, January 15, 2007 7:02:38 AM Imar Spaanjaars said:
Hi surekha,

How is this related to passing credentials to a web service, the topic of this snippet?

Try a forum like instead.


On Wednesday, October 10, 2007 5:12:21 PM Curtis said:
I am and administrator of a SharePoint portal, it is secured with ssl and we are using basic authentication and cac authentication.  I would like to create a web page that will pass username and password or CAC credentials to our portal.  I'm not even sure where to begin.  Any help would be apreciated.
On Thursday, May 15, 2008 11:09:13 AM kumar said:
Hi Imar,

I am trying to call a webserice with basic authentication using XMLHttpRequest, do you know how to pass the credentials?
I have tried different techniques (passing credentials with the open method, adding headers using setRequestHeader  ) none of them worked !

On Saturday, May 17, 2008 8:51:08 AM Imar Spaanjaars said:
Hi kumar,

Sorry, no idea. I thought the user and password in the Open method would work.


On Wednesday, January 14, 2009 5:59:41 AM mooo993 said:
usefull refrence for this--
On Wednesday, April 22, 2009 4:24:05 AM shanwaj said:
Hi Imar,
    I want to send a html page as web mail. It is sending login.aspx page instead of current web page.My folders are fully authenticated.I put my problem in suggested me that I need to authenticate  the web page aslo,I am not getting how can I authenticate  my webpage which is sending as email.Please suggest me how can I do this.
Thanks in advance
On Wednesday, April 22, 2009 6:36:54 PM Imar Spaanjaars said:
Hi shanwaj,

If you're already talking to Lee on P2P, may I suggest you continue the discussion over there? It's quite off-topic here....

On Monday, July 27, 2009 7:25:39 PM Shrikant Landge said:
I was searching for the same. Thank you ..
On Thursday, April 28, 2011 9:52:08 PM somasekhar akiri said:
nice article i am really appreciated it.
On Thursday, June 9, 2011 5:28:45 AM Drew said:
Anyone had to do this using Visual Studio 2010?  For some reason the myService.Credentials is missing.
On Thursday, June 9, 2011 5:32:58 AM Imar Spaanjaars said:
Yep, it seems to be fine in .NET 4 for me...


On Thursday, February 9, 2012 3:22:50 PM avidDev said:
Hey Imar,

Do you have to package the NetworkCredentials object inside a CredentialCache object?  Since both implement ICredentials, wouldn't the following work:

      NetworkCredential cred = new NetworkCredential();
      cred.UserName = username;
      cred.Password = password;
      myService.UseDefaultCredentials = false;
      myService.Credentials = cred;

I ask because I just set this up in a few calling clients, and although it appears to work, I'd like to prevent problems down the road...


On Sunday, February 12, 2012 4:09:18 PM Imar Spaanjaars said:
Hi there,

Not sure. The CachedCredentials may perform a little faster if you have multiple calls. However, in this example it should matter, I think.....



Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.