Solved

Basic Authentication of Web Services

Posted on 2006-11-07
6
260 Views
Last Modified: 2011-10-03
I'm using C# / Visual Studio to access a web service. The service requires basic authentication to show its' available operations. Passing the username and password to it works well. Adding the reference to my project does also work well. But when accessing operations in my code, I get a 401 error, saying "Invalid authentication".

So, my question: How do I configure my service to pass the username and password at runtime?
0
Comment
Question by:jarleberge
  • 2
6 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17891444
have you generated a proxy class for the service using WSDL ?
If so you can edit that class and add fields for the username and password that can be set either using accessor methods or from a constructor.

or you can do it on the fly by creating an instance of your proxy class but before you call ant of its methods you set its credentials :

proxyclass.Credentials = new NetworkCredential(username, password);
0
 

Author Comment

by:jarleberge
ID: 17896658
Thank you!

I'm not a very experienced .NET programmer, so I can sketch up my approach in some steps here:

1) Adding a Web Reference in my Visual Studio by giving the adress of the WSDL
2) I'm provided to give a username and password
3) A list of operations appears and I give the reference a name and add it
4) I use the reference directly in my code calling the operations with neccesary parameters and handling the result.

The problem comes at step 4), when running it. How can I actually make the proxy to let it handle username and password for the operation/method calls. Do you have any more explaining code?

0
 
LVL 15

Accepted Solution

by:
ozymandias earned 250 total points
ID: 17896702
There is a commandline tool that comes with .NET framework called WSDL.exe.
You run this and point it at a webservice URL.
It will generate a proxy class derived from System.Web.Services.Protocols.SoapHttpClientProtocol.

The proxy class with have async proxies for all the methods of the webservice.
If you name your proxy class MyWebService and it has a method called GetData(), you cxan call it from your code like this :

MyWebService service = new MyWebService();
service.GetData();

Becuase the proxy is derived from System.Web.Services.Protocols.SoapHttpClientProtocol you can set its credentials like this :

MyWebService service = new MyWebService();
service.Credentials = new NetworkCredential(username, password);

// and then call the method
service.GetData();

Alternatively, you could hard code the username and password into the proxy class by editing its constructor.

public MyWebService() {
    this.Url = "WebServiceURL";
    /// add this line below
    this.Credentials = new NetworkCredential(usernme, password);
}

That way everytime you create an instance of your proxy class it will have its credentials ready to go.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now