Once you follow the above configuration steps, you can easily
access the logged-in username and role/group mappings for the authenticated
user within ASP.NET. For example, you could use the code-snippet below
within an ASP.NET page to easily obtain the username of the visiting user:
Dim username As String
username = User.Identity.Name
The code-snippet above works because there is a “User”
property built-in to all ASP.NET pages and user-controls. If you want to
gain access to this user data from within a regular class or business object
(which doesn’t have this property provided), you can write code like below to
achieve the same result:
Dim User As System.Security.Principal.IPrincipal
User = System.Web.HttpContext.Current.User
Dim username As String
username = User.Identity.Name
The code above obtains the User IPrincipal object for the
current request by accessing it via the static HttpContext.Current property
that ASP.NET provides (this in turn uses call-context to retrieve it from the
active ASP.NET worker thread). This avoids you having to pass this User
object into your business classes directly, and instead allows you to access
the User object from anywhere in the application.