Walkthroughs: HTTP Digest on the Client Side

C# and VB.NET Code is available
when installing the Samples package
Open this document in seperate window

In this walkthhrough, you will:

When using the .NET Framework, implementing HTTP Digest on the client side of your distributed applications could hardly be any easier. This walkthrough will take you through setting up a Web Service reference to a protected Service in Visual Studio .NET and show how few lines of code are necessary to authenticate a client.

RestrictedWs.asmx

We will be referencing and accessing the basic Web Service RestrictedWs.asmx, which we created in the earlier Basic and Advanced walkthrough.

  1. In Visual Studio .NET, use the well-known Add Web Reference... dialog to set-up the Web Service

  1. When requesting the auto-generated WSDL of a protected Web Service, Visual Studio .NET will also prompt you for credentials.
    As you can see, your authentication mechanism works everywhere!

Notice again the Realm IBuySecurely, which was specified in the configuration of the Kabel .NET module for this web application.

  1. Visual Studio .NET's built-in WSDL discovery tool will also prompt for credentials
    (It makes requests independently of the first prompt, so no faults on your part)

In this dialog, you are required to enter the Realm (IBuySecurely) manually. Notice that it is referred to as Domain here.

  1. Ok!

The 1-Liner

The code needed to call the restricted service is literally only 1 line longer than for a normal service: we merely need add a System.Net.NetworkCredential via the Credential property of the service's proxy class.

 

401 Exception Handling

Now when calling a secured service, you may want to implement additional exception handling to handle possible HTTP 401. Access Denied responses. This can simply done as follows:

Exception Handling - C#

localhost.RestrictedWs kabelWs = new localhost.RestrictedWs();

kabelWs.Credentials = new NetworkCredential(username, password);
// Assigning credentials here.

try
{

Console.WriteLine("HelloCurrentUser() successfully returned: {0}\n",
kabelWs.HelloCurrentUser()); // Calling the Service here

}
catch (System.Net.WebException x)
{

if (x.Status == WebExceptionStatus.ProtocolError)
// This Status is most likely a 401 (Access Denied) response here

Console.WriteLine("The Request was rejected: {0}", x.Message);

else

throw; // Just let the next catch block handle this exception


}
catch (Exception x) // General Exception occured
{

Console.WriteLine("An Error occured while calling the Web Service: {0}\n", x.Message);

}

You make the Call

Below we make two seperate requests:

  1. In the first, we are passing invalid credentials and our request is denied and the response exception handled as seen above.

  1. In the second one, we use the credentials admin with secret-password123. The Module successfully authenticates us, ASP.NET moves on to process the XML request and the Service returns the name of our authenticated identity (Context.User.Identity.Name)

Feedback on Help
Copyright © 2002, uthentic.net
All Rights Reserved