Recipe: Enabling Windows Authentication within an Intranet ASP.NET Web application
page 5 of 10
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36507/ 112

Obtaining the Logged-in Username via Code

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.


View Entire Article

User Comments

Title: Help please   
Name: Kaustubh
Date: 2008-09-02 3:05:57 AM
Comment:
Hello,

I did same thing as u mentioned. But when I tried to open it through browser. it asks for windows user and password.

What could be reason ?
Title: HELP !   
Name: Bruce
Date: 2007-12-03 4:56:27 PM
Comment:
I've done this. When the browser is run from a W2K machine, I get "Cannot find server or DNS Error". When run from an XP machine, it appears to work just fine. Any ideas? Thanks.
Title: how to implement windows integrated authentication in java step by step   
Name: Shilpa
Date: 2007-08-16 3:12:57 AM
Comment:
Can I retrieve windows authentication in java
Title: Hi   
Name: Hello
Date: 2007-05-17 4:20:29 PM
Comment:
Super

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 1:54:38 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search