In this article, we will develop a .NET Web Service in C# that requires
The Web Service will expose the standard “Hello World” web method, and for the
client to be able to receive the hello, it will need to send its credentials in
the form of a username and password sent along with the SOAP call.
Lets build the Service first.
Using Visual Studio, I am going to create a new project called
“HelloWorldAuthorized” that is a ASP.NET Web Service in C#.
Now lets use the Service1.asmx class to implement our Web Service method. Here
is the code we will add:
The AuthHeader class is the class that will hold the authorization information,
and should be public to allow the client that consumes this web service to
access the class. This class is a child class of SoapHeader. The client will
populate its fields and send it to the service with the public variable
class AuthHeader : SoapHeader
public string UserName;
public string UserPassword;
public AuthHeader Header;
The actual Web Service that does the work is the HelloWorld() method as
We will need to import the System.Web.Services.Protocal namespace into our
Now we will code our Web Method:
public string HelloWorld()
if (Header.UserName.ToLower() == "harrison")
return "Hello World!";
return "you are not
We have out standard [WebMethod] attribute and the [SoapHeader] to specify that
we expect the authorization information to be passed to this Web Method. In
addition, we are going to check this authorization information that we receive.
For this one, I am only checking the Username, not the password, and I am
converting it all “ToLower()” to eliminate the case sensitivity. Of course, in
the real world it would be best to add some additional security to this.
Now for the Client:
I am going to add a new Visual Studio Project to my solution called
“HelloWorldClient” that is an ASP.NET Web Application, in C#.
First, I will need to add a Web Reference to my Service I created above. To do
that in Visual Studio, right-click the “HelloWorldClient” project and choose
“add a Web Reference.” In the URL box, enter the URL to our Web Service,
“http://localhost/HelloWorldAuthorized/Service1.asmx.” You should then see the
service description and then click “Add Reference” to add it to our
project. You should now see the reference show up as “localhost”
under the Web References section of the Visual Studio Solution Explorer.
Now for the code.
For our implementation, I added a Label control onto the WebForm1.aspx page
created in our HelloWorldClient project, called Label1. Then, in the Page_Load
event, I added the call to the Web Service, as shown below:
private void Page_Load(object
sender, System.EventArgs e)
localhost.AuthHeader auth =
auth.UserName = "Harrison";
auth.UserPassword = "password";
localhost.Service1 ws =
ws.AuthHeaderValue = auth;
Label1.Text = ws.HelloWorld();
First, we create an instance of the AuthHeader class on the client, called
“auth,” and populate the username and password. Then, we create an instance of
the Web Service as the variable “ws.” We attach the header using the Service’s
public property AuthHeaderValue, then make the call, and return the results to
If we browse to the Client file through the browser, we will see the results.
So what we have accomplished, is to create is a Web Service that requires a
username for authentication. This happens in the real world with web services,
some require a key string, like the Google API, and others require that you
have an account.
Thanks for reading and to see the complete code, you can download the source project at http://www.harrisonlogic.com/HL.