Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current user's email address in .NET [closed]

I would like to know the email address of the user (assuming she's in a typical Windows office network). This is in a C# application. Perhaps something to the effect of

CurrentUser.EmailAddress; 
like image 336
Calv1n Avatar asked Sep 09 '11 04:09

Calv1n


2 Answers

Reference System.DirectoryServices.AccountManagement, then

using System.DirectoryServices.AccountManagement;
return UserPrincipal.Current.EmailAddress;

See .NET docs UserPrincipal.Current and UserPrincipal.EmailAddress.


Or with a timeout:

var task = Task.Run(() => UserPrincipal.Current.EmailAddress);
if (task.Wait(TimeSpan.FromSeconds(1)))
    return task.Result;
    
like image 142
Colonel Panic Avatar answered Nov 11 '22 08:11

Colonel Panic


If you're behind a Windows domain, you could always grab their email address out of Active Directory.

See Javier G. Lozano's example in his tutorial, "Querying Active Directory for User Emails".

like image 5
Ryan Avatar answered Nov 11 '22 10:11

Ryan