Team LiB
Previous Section Next Section

How To—Implement IPrincipal

Overview

The .NET Framework provides the WindowsPrincipal and GenericPrincipal classes, which provide basic role-checking functionality for Windows and non-Windows authentication mechanisms respectively. Both classes implement the IPrincipal interface. To be used for authorization, ASP.NET requires that these objects are stored in HttpContext.User. For Windows-based applications, they must be stored in Thread.CurrentPrincipal.

The functionality offered by these classes is sufficient for most application scenarios. Applications can explicitly call the IPrincipal.IsInRole method to perform programmatic role checks. The Demand method of the PrincipalPermission class, when used to demand that a caller belong to a particular role (either declaratively or imperatively) also results in a call to IPrincipal.IsInRole.

In some circumstances, you might need to develop your own principal implementations by creating a class that implements the IPrincipal interface. Any class that implements IPrincipal can be used for .NET authorization.

Reasons for implementing your own IPrincipal class include:

This How To describes how to implement a custom IPrincipal class and use it for role-based authorization in an ASP.NET application that uses Forms authentication.


Team LiB
Previous Section Next Section