5

I am designing a web application using the ASP.net MVC framework. I would like to use Windows Authentication and do Role Checks using the Role Manager SQLRoleProvider.

How can I determine the email address of the current logged on user? Is this even possible?

The application will be deployed in a multi-domain intranet, if that matters (which I assume it does).

Thanks for any help!

carter
  • 629
  • 3
  • 8
  • 15

4 Answers4

13

Here is a sample from some code:

DirectorySearcher searcher = new DirectorySearcher();    
searcher.Filter = string.Format("sAMAccountName={0}", _name);    
SearchResult user = searcher.FindOne();    
string emailAddr = user.Properties["mail"][0].ToString();
Mark Kadlec
  • 8,036
  • 17
  • 60
  • 96
4

You can look up the user's properties in Active Directory. Here is a great article that explains how to do that using System.DirectoryServices and C#:

http://www.codeproject.com/Articles/6778/How-to-get-User-Data-from-the-Active-Directory

TimS
  • 5,922
  • 6
  • 35
  • 55
  • **This link is no longer valid. Often it is suggested to not use links for the very reason. Please post example code. (for @Raiden Flyboy) – Boris Callens Apr 29 '15 at 13:54
0

In MVC5 application give the action as given below

  public ActionResult Index()
        {
            string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            ViewBag.Name = name;
            ViewBag.Email = uEmail(User.Identity.Name.Replace(@"YOURDOMAIN\", ""));
            return View();
        }

Change the YOURDOMAIN to your domain name. And give the uEmail function as given below. Also add the reference to using System.Security.Principal and using System.DirectoryServices.

private string uEmail(string uid)
        {
            DirectorySearcher dirSearcher = new DirectorySearcher();
            DirectoryEntry entry = new DirectoryEntry(dirSearcher.SearchRoot.Path);
            dirSearcher.Filter = "(&(objectClass=user)(objectcategory=person)(mail=" + uid + "*))";

            SearchResult srEmail = dirSearcher.FindOne();

            string propName = "mail";
            ResultPropertyValueCollection valColl = srEmail.Properties[propName];
            try
            {
                return valColl[0].ToString();
            }
            catch
            {
                return "";
            }

        }
Mahesh ML
  • 542
  • 9
  • 16
  • 2
    I think the user name not equal to email account even you remove the domain – MichaelMao Aug 30 '16 at 03:03
  • @MichaelMao I think he is not removing the domain only, at the same time invoking the 'uEmail' method, where it bring the email address from the AD – tareq aziz Oct 24 '21 at 21:37
  • Pretty sure that `System.Security.Principal.WindowsIdentity.GetCurrent()` doesn't give you the identity of the client, but the user hosting the website... – Pieterjan Sep 12 '22 at 09:09
-1

The asp.net membership services database is just a database, which you can execute a query against directly. I dont think the default membership provider has a way to get the email address however.

Nick Larsen
  • 18,631
  • 6
  • 67
  • 96