11

I am using windows Authentication and accessing user name as.

IIdentity winId = HttpContext.Current.User.Identity;
string name = winId.Name;

but i want to get other details like User full name and EmailID.

2
  • Are you using memebership provider in your application? Commented Oct 21, 2010 at 9:59
  • no, my app. is on intranet so using windows authentication. Commented Oct 21, 2010 at 10:02

3 Answers 3

13

Since you're on a windows network, then you need to query the Active directory to search for user and then get it's properties such as the email

Here is an example function DisplayUser that given an IIdentity on a windows authenticated network, finds the user's email:

public static void Main() {
    DisplayUser(WindowsIdentity.GetCurrent());
    Console.ReadKey();    
}

public static void DisplayUser(IIdentity id) {    
    WindowsIdentity winId = id as WindowsIdentity;
    if (id == null) {
        Console.WriteLine("Identity is not a windows identity");
        return;
    }

    string userInQuestion = winId.Name.Split('\\')[1];
    string myDomain = winId.Name.Split('\\')[0]; // this is the domain that the user is in
     // the account that this program runs in should be authenticated in there                    
    DirectoryEntry entry = new DirectoryEntry("LDAP://" + myDomain);
    DirectorySearcher adSearcher = new DirectorySearcher(entry);

    adSearcher.SearchScope = SearchScope.Subtree;
    adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))";
    SearchResult userObject = adSearcher.FindOne();
    if (userObject != null) {
        string[] props = new string[] { "title", "mail" };
        foreach (string prop in props) {
            Console.WriteLine("{0} : {1}", prop, userObject.Properties[prop][0]);
        }
    }
}

gives this: alt text

Edit: If you get 'bad user/password errors' The account that the code runs under must have access the users domain. If you run code in asp.net then the web application must be run under an application pool with credentials with domain access. See here for more information

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, but ... I am using windows authentication so my purpose is: there is a usl "ipAddress/myApp/home.aspx" now when use open it on intra net then in screen we will see His login name, Full name and emailid. He will not do anythink else. Now come to ur code... how will we set "myPassword".
Alt+PrintScreen will capture only selected windows so you will not have background noise on the resulting screenshot
@Preet: in local (bebug) it is working fine but in iis its giving error...System.DirectoryServices.DirectoryServicesCOMException: Logon failure: unknown user name or bad password.
-1

You can define a MyCustomIdentity by overriding from IIdentity and add your own properties etc.

Comments

-2

Cast it to the specific Identity, for example WindowsIdentity

1 Comment

i think u have not read my question. I need full name and emailid. We can not get these details by WindowsIdentity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.