Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Basic Authentication of Web Services

Posted on 2006-11-07
6
Medium Priority
?
268 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 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

885 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