Walkthroughs: Basic

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

In this walkthhrough, you will:

With the Basic implementation method, your code will capture the HTTP Module’s Authenticate event using the global.asax file of your web application. You will need to validate the given username and respond with either the stored password or a notice that the username is invalid by itself (for example, if it does not exist or the associated account is disabled). See How Kabel .NET works to better understand this concept.

Getting Started: Enabling Kabel .NET

To jumpstart, let’s first specify the minimum configuration settings in the web.config file of your ASP.NET application to enable the Kabel .NET module.

web.config

<configuration>
<system.web>

...

<httpModules> [1]

<add name="HttpDigestModule" [1.1]
type="uthentic.Web.Security.HttpDigest.DigestAuthenticationModule,
uthentic.Web.Security.HttpDigest,
Version=1.0.0.0, Culture=neutral,
PublicKeyToken=e26cf364916fe0b7" />

</httpModules>

<authentication mode="None" /> [2]

</system.web>

<uthentic.HttpDigest>
<authentication realm="IBuySecurely" /> [3]
</uthentic.HttpDigest>

</configuration>

Overriding IIS Security settings

Lastly, it is necessary to disable the Integrated Windows authentication of IIS, because otherwise IIS will attempt to authenticate your HTTP Digest clients, which will only fail. Follow these simple steps:

  1. Open your IIS Virutal Directory settings and go to the Directory Security tab

  1. Click Edit... and the upcoming dialog, uncheck Integrated Windows Authentication

Adding a Reference to the global Kabel .NET assembly

You will need to do add a reference to the Kabel .NET assembly, which contains the necessary classes that you will be using in just a moment. To do this in Visual Studio .NET, simply follow these steps:

Implement Your Code!

Now we are ready to get coding. We will capture the Authenticate event, which the Kabel .NET module sends whenever it needs to authenticate a client request. This is done at the application level of in the global.asax file. The basic method body looks as follows:

Authentication in global.asax - C#

protected void HttpDigestModule_Authentication(object source, AuthenticationEventArgs e)
{

// Your Code will come here

}

Authentication in global.asax - VB.NET

Protected Sub HttpDigestModule_Authentication(source As Object, e As AuthenticationEventArgs)

' Your Code will come here

End Sub

 

When implementing the event handler, make sure that it is named as follows:
FriendlyName_Authentication where FriendlyName is the name assigned to the Module in [1.1] above.

 

The most important part: AuthenticationEventArgs

The AuthenticationEventArgs parameter (named e in the above snippet) passed into this method is your way of communicating with the underlying Kabel .NET module. This parameter exposes the following properties:

It also exposes two important methods, namely:

Bringing it together

Now that the AuthenticationEventArgs is explained, let's look at how to use it. The following example implements the most simple Username validation, but it does not require much imagination to see a more sophisticated authentication being implemented instead (such as database lookups, or perhaps querying an XML configuration files, etc.)

Simple Username Validation - C#

using uthentic.Web.Security.HttpDigest;
using uthentic.Web.Security.HttpDigest.Events;

// for namespace abbreviations when later referencing
// the AuthenticationEventArgs and DigestHelper classes

...

protected void HttpDigestModule_Authentication(object source, AuthenticationEventArgs e)
{

if (e.Username.Equals("admin"))
{

e.UsernameAccepted("secret-password123");
// The valid password for User 'admin' is 'secret-password123'

}
else if (e.Username.Equals("mickey"))
{

e.UsernameAccepted("mouse-password");
// The valid password for User 'mickey' is 'mouse-password'

}
else
{

e.UsernameRejected();
// We do not know any other users. Reject this one.

}

}

Nothing easier than that.

NOTE: With HTTP Digest, passwords are case-sensitive. This is an important detail on the client side.

Use the DigestHelper class

The uthentic.Web.Security.HttpDigest contains a class called DigestHelper, which exposes many useful functions related to Authentication and HTTP Digest processing. We will use it here to construct a simple User object that we will pass back into the e.UsernameAccepted method.

All methods of DigestHelper class are static / Shared; think of them as utility functions to simplify your development tasks. See the API Documentation for more information.

Using the DigestHelper.MakeSimpleUser and the optional IPrincipal user argument - C#

using uthentic.Web.Security.HttpDigest;
using uthentic.Web.Security.HttpDigest.Events;

// for namespace abbreviations when later referencing
// the AuthenticationEventArgs and DigestHelper classes

...

protected void HttpDigestModule_Authentication(object source, AuthenticationEventArgs e)
{

if (e.Username.Equals("admin"))
{

e.UsernameAccepted ( "secret-password123",
DigestHelper.MakeSimpleUser ("Adminstrator") );

// We are associating the name 'Administrator'
// with the identity of the Client

}
else if (e.Username.Equals("mickey"))
{

e.UsernameAccepted ( "mouse-password",
DigestHelper.MakeSimpleUser ("MickeyMouse") );

// ... or 'MickeyMouse' in this case

}
else
{

e.UsernameRejected();
// We do not know any other users. Reject this one.

}

}

The User, which we thus assigned above, can later be used to identify the Client's identity within the code of ASP.NET WebForms and Web Services; it accessed via Context.User. To learn more about the Context.User object (Type: IPrincipal), check out the MSDN .NET Framework documentation. It can add significantly to your application logic by simplifying the identification, role assignments, and permissions of the current user within your code. Click here for a Google search that should always get you a working link to the official documentation.

To learn more about the functions available in the DigestHelper, please refer to the API Documentation.

At last: Kabel .NET in Action

So far, we have successfully

This means, we are ready to see Kabel .NET in action. Let's create a simple Web Service, which will only be accessible to successfully authenticated users and return the name of the current user (i.e. "Administrator" or "MickeyMouse", as seen above). The following snippet defines our public WebMethod:

Simple ASP.NET Web Service: Restricted.asmx- C#

[WebMethod]
public string HelloCurrentUser()

{

return "Hello, " + Context.User.Identity.Name;
// This will return the name previously assigned to the Current User

}

Let's call this serivce Restricted.asmx, and since it is restricted, let's configure the application so that only authenticated will be able to call it.

ASP.NET supports a powerful configuration mechanism, which allows you to authorize users according to their verified identity, roles, etc. on a ressource-specific basis. (Authorization happens after successful Authentication; at this stage, an application determines whether the authenticated user actually has the permission to access a given ressource.)

You can restrict all of your Web Application to authenticated users, by using the web.config <authorization> tag. Alternatively, you can use the <location> tag to specify different access criteria for individual pages and services.

The following web.config snippet configures the application so that all pages are restricted to authenticated users using the <authorization> tag:

web.config with <authorization> tag

<configuration>
<system.web>

...

<authorization>

<deny users="?" /> [4]

</authorization>

<httpModules> [1]

<add name="HttpDigestModule" [1.1]
type="uthentic.Web.Security.HttpDigest.DigestAuthenticationModule,
publickey=..." />

</httpModules>

<authentication mode="None" /> [2]

</system.web>

<uthentic.HttpDigest>
<authentication realm="IBuySecurely" /> [3]
</uthentic.HttpDigest>

</configuration>

 

The next possible web.config code configures the application so that only Restricted.asmx will be restricted; we are using the <location> tag:

web.config with <location> tag

<configuration>

<location path="Restricted.asmx">

<sytem.web>
<authorization>
<deny users="?" /> [5]
</authorization>
</sytem.web>

</location>


<system.web>

...

<httpModules> [1]

<add name="HttpDigestModule" [1.1]
type="uthentic.Web.Security.HttpDigest.DigestAuthenticationModule,
publickey=..." />

</httpModules>

<authentication mode="None" /> [2]

</system.web>

<uthentic.HttpDigest>
<authentication realm="IBuySecurely" /> [3]
</uthentic.HttpDigest>

</configuration>

Let's Launch

Let's test our simple but secure application: Open up Internet Explorer and point it to the location of Restricted.asmx on your web server. The following screenshots illustrate a simple access scenario:

  1. When accessing the protected resource, Internet Explorer prompts for credentials.
    Notice the IBuySecurely identifier, as specified for the Web Application's Realm in [3] of the web.config file.

  1. First, when entering bad credentials (Username: i-am-a-hacker), we are denied access:

  1. Next, we try the accepted Username mickey with the password mouse-password. See there...

we're in!

  1. The HelloCurrentUser method of the Service then returns the following user-dependent string:

  1. Notice also that no additional re-authentication was required during the session; any possibly expired or stale tickets will be automatically renewed through the communication between Internet Explorer and Kabel .NET
  2. See the HTTP Digest Client Side Walkthrough for an example of how to automate authentication in your client applications.

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