Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Basic Authentication of Web Services

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

861 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