Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get UPN or email for logged in user in a .NET web application

Posted on 2009-07-08
1
Medium Priority
?
1,069 Views
Last Modified: 2012-05-07
I have an ASP. NET web application that makes user of the user credentials of the logged in user. Currently it uses the SID which comes from

System.Security.Principal.WindowsIdentity.GetCurrent().User.Value

I need to get either the users UPN login or email address (as defined in active directory) instead of the SID. GetCurrent() returns an object of type WindowsIdentity; looking in the details for WindowsIdentity Members:

http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity%5Fmembers.aspx

I can't see anything that looks like it would give me either the UPN or email in there. How can I pull up that information to use, either by feeding the SID into some other function or calling something different in the first place.

System.Security.Principal.WindowsIdentity.GetCurrent().Name returns "DOMAIN\user", so that is not the answer.

0
Comment
Question by:DrStalker
1 Comment
 
LVL 12

Accepted Solution

by:
Hairbrush earned 2000 total points
ID: 24811676
Try this, which requires System.DirectoryServices.


   private string CurrentUserEmail()
   {
		string tempCurrentUserEmail = null;
 
		//Returns the email address of the current user from Active Directory
 
		string sCurrentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
 
		sCurrentUser = sCurrentUser.Substring(sCurrentUser.IndexOf("\\") + 1);
		DirectoryEntry Entry = new DirectoryEntry("LDAP://RootDSE");
		string sFQDN = System.Convert.ToString(Entry.Properties["defaultNamingContext"].Value);
		DirectoryEntry myDE = new DirectoryEntry("LDAP://" + sFQDN);
 
		DirectorySearcher mySearcher = new DirectorySearcher(myDE);
 
		mySearcher.Filter = "sAMAccountName=" + sCurrentUser;
		mySearcher.PropertiesToLoad.Add("Mail");
		try
		{
			SearchResult myresult = mySearcher.FindOne();
			tempCurrentUserEmail = System.Convert.ToString(myresult.Properties["Mail"][0]);
		}
		catch (Exception ex)
		{
			throw new ApplicationException("Could not establish an email address for user " + sCurrentUser);
		}
 
		return tempCurrentUserEmail;
	}

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question