Solved

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

Posted on 2009-07-08
1
879 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I get the id from URL? 19 48
Set form below another form 3 26
c# if statement weird reaction 3 30
Jquery autocomplete 10 31
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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now