Solved

Basic Authentication of Web Services

Posted on 2006-11-07
6
264 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
[X]
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
  • 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Office 1 67
how to check mis-spellings in a select SQL 2 53
Cannot interpret token '"' at position 10. 3 25
Disable the weekends on datepicker control 6 51
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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