Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user details in asp.net Windows Authentication

Tags:

asp.net

I am using windows Authentication and accessing user name as.

IIdentity winId = HttpContext.Current.User.Identity;
string name = winId.Name;

but i want to get other details like User full name and EmailID.

like image 966
Jeevan Bhatt Avatar asked Oct 21 '10 09:10

Jeevan Bhatt


People also ask

How do I access Windows Authentication?

On the taskbar, click Start, and then click Control Panel. In Control Panel, click Programs and Features, and then click Turn Windows Features on or off. Expand Internet Information Services, then World Wide Web Services, then Security. Select Windows Authentication, and then click OK.

How do I find my IIS username?

Right-click on the E-WMS virtual directory name and choose Properties, tab 'Directory Security' and press Edit: In this case, the IIS user name is "IUSR".

How do you implement Windows Authentication How do you specify roles and permissions to the users?

To set up your ASP.NET application to work with Windows-based authentication, begin by creating some users and groups. Within your Windows operating system, go to "Control Panel" -> "User Accounts" -> "Manage another account" -> "Create a new account" then choose "Add or Remove User".


1 Answers

Since you're on a windows network, then you need to query the Active directory to search for user and then get it's properties such as the email

Here is an example function DisplayUser that given an IIdentity on a windows authenticated network, finds the user's email:

public static void Main() {
    DisplayUser(WindowsIdentity.GetCurrent());
    Console.ReadKey();    
}

public static void DisplayUser(IIdentity id) {    
    WindowsIdentity winId = id as WindowsIdentity;
    if (id == null) {
        Console.WriteLine("Identity is not a windows identity");
        return;
    }

    string userInQuestion = winId.Name.Split('\\')[1];
    string myDomain = winId.Name.Split('\\')[0]; // this is the domain that the user is in
     // the account that this program runs in should be authenticated in there                    
    DirectoryEntry entry = new DirectoryEntry("LDAP://" + myDomain);
    DirectorySearcher adSearcher = new DirectorySearcher(entry);

    adSearcher.SearchScope = SearchScope.Subtree;
    adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))";
    SearchResult userObject = adSearcher.FindOne();
    if (userObject != null) {
        string[] props = new string[] { "title", "mail" };
        foreach (string prop in props) {
            Console.WriteLine("{0} : {1}", prop, userObject.Properties[prop][0]);
        }
    }
}

gives this: alt text

Edit: If you get 'bad user/password errors' The account that the code runs under must have access the users domain. If you run code in asp.net then the web application must be run under an application pool with credentials with domain access. See here for more information

like image 89
Preet Sangha Avatar answered Oct 07 '22 04:10

Preet Sangha