tags:

views:

62

answers:

4

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.

A: 

Cast it to the specific Identity, for example WindowsIdentity

Peter van Kekem
i think u have not read my question. I need full name and emailid. We can not get these details by WindowsIdentity.
Jeevan Bhatt
A: 

You can cast HttpContext.Current.User.Identity to concrete type (e.g. WindowsIdentity) or implement custom Identity and cast to it.

So you code will be looking like this:

MyCustomIdentity identity = (MyCustomIdentity)HttpContext.Current.User.Identity;
var email = identity.Email;
Restuta
read question completely, currently m using IIdentity which gives only name not full name and emailid.
Jeevan Bhatt
How to define MyCustomIdentity ? share code
Jeevan Bhatt
+2  A: 

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

Preet Sangha
Jeevan. Does this example not work for you?
Preet Sangha
thanks, but ... I am using windows authentication so my purpose is: there is a usl "http://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".
Jeevan Bhatt
have edited the answer to make it simpler. Is that ok now?
Preet Sangha
Alt+PrintScreen will capture only selected windows so you will not have background noise on the resulting screenshot
abatishchev
Thanks that was me using snag it. It's quite cold tonight and I'm shivering...
Preet Sangha
@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.
Jeevan Bhatt
A: 

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

Peter van Kekem