Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-07-08
1
Medium Priority
?
1,029 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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