Solved

Basic Authentication of Web Services

Posted on 2006-11-07
6
259 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

13 Experts available now in Live!

Get 1:1 Help Now