Accessing the email address of the current logged user from Windows service running as local system

Hello,

In this post, I will show you how to retrieve the current logged user email from a Windows service, running as local system in an entreprise architecture, using an active directory.

As you probably know, you can’t access the user entity directly from the Windows service, so we have to be smart and find a workaround 🙂

First here is to retrieve the current logged username (DOMAIN\user). This method will get the owner of the explorer.exe process, so this is basically the current logged user.

The code below explain this process :

 ///
 /// Method to get the current logged username via the explorer.exe owner process name
 ///
 string GetLoggedUserName() {
 	string query = "Select * from Win32_Process Where Name = \"" + "explorer.exe" + "\"";
 	ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
 	ManagementObjectCollection processList = searcher.Get();
 	foreach(ManagementObject obj in processList) {
 		string[] argList = new string[] {
 			string.Empty, string.Empty
 		};
 		int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
 		if (returnVal == 0) { // return DOMAIN\user string owner = argList[1] + "\\" + argList[0]; return owner; } } return "NO OWNER"; }
 		}
 	}
 }

In the second step, we must now retrieve from the active directory the email address of the username.

The code below use a LDAP query to retrieve this information.

///
 /// method to retrieve the email address of the current logged user on the machine
 ///
 /// domain name of the current logged user 
 /// user name of the current logged user 
 /// a string, the user email address 
 private string getCurrentUserMail(string domain, string username) {
 	try {
 		const string pathNameDomain = "LDAP://" + domain;
 		var direcotyEntry = new DirectoryEntry(pathNameDomain);
 		var directorySearcher = new DirectorySearcher(direcotyEntry) {
 			Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"
 		};
 		var searchResults = directorySearcher.FindAll();
 		foreach(SearchResult searchResult in searchResults) {
 			return searchResult.Properties["mail"][0].ToString();
 		}
 		direcotyEntry.Dispose();
 		directorySearcher.Dispose();
 		searchResults.Dispose();
 	} catch (InvalidOperationException iOe) {
 		return "NOT FOUND";
 	} catch (NotSupportedException nSe) {
 		return "NOT FOUND";
 	} catch (Exception ex) {
 		return "NOT FOUND";
 	}
 }
Advertisements
This entry was posted in C# Solutions and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s