C# .NET - not getting active directory user details

Asked By jidheesh Rajan on 06-Dec-12 05:08 AM


Hi,

I am not getting the Active directory user details after hostig my website in local iis (varsion5.1).  Can anyone help me??


thanks
Jidheesh
Robbe Morris replied to jidheesh Rajan on 06-Dec-12 09:33 AM
You won't unless you set the IIS application NOT to use anonymous authentication.  It must use windows authentication only.
jidheesh Rajan replied to Robbe Morris on 07-Dec-12 08:18 AM



I did what u suggested. The details i am getting from AD. But I need to enter the user name and password. Is there any option to get the details with out entering the password. So that i can avoid my login screen.
Robbe Morris replied to jidheesh Rajan on 07-Dec-12 08:20 AM
jidheesh Rajan replied to Robbe Morris on 09-Dec-12 11:35 PM

Hi, Thank you so much for supporting me...

I referred the link , in the function
 public static bool LoadSession(string userName, string password, string domainControllerServerName)

you are passing the password. But I don't want to pass the password to AD. I need to  get the Currently logged in  Active directory user details with out entering the password. Could you please have a look on this and help me?

Thanks in advance
jidhu  
Robbe Morris replied to jidheesh Rajan on 10-Dec-12 08:28 AM
Read closer.  I just used it to validate their credentials.  You do not need to do that.  You need the code below it.
jidheesh Rajan replied to Robbe Morris on 11-Dec-12 08:04 AM
I tried that code. its working fine in Visual studio development server. But after hosting to IIS, the user name is displaying as
ASPNET and not getting any other AD Details. I am using IIS5.1 . Please have a look on this and help me.
Robbe Morris replied to jidheesh Rajan on 11-Dec-12 08:04 AM
Are you hard coding the user's username or are you attempting to get it from the Request.ServerVariables collection?
jidheesh Rajan replied to Robbe Morris on 12-Dec-12 01:20 AM


Thank you very much for your support.

I am getting the username with this code.

private String GetLoggedUser()
        {
            string Username = Page.User.Identity.Name;
            int hasDomain = Username.IndexOf(@"\");
            if (hasDomain > 0)
            {
                Username = Username.Remove(0, hasDomain + 1);
            }
            return Username;
        }


And i am trying to get the AD Details with following code

string connection = "LDAP://SEDC.HELLO.COM";
            String domainAndUsername = "HELLO" + @"\" + GetLoggedUser();
          using (DirectoryEntry entry = new DirectoryEntry(connection))
            {
                string UserName = GetLoggedUser();
                try
                {
                    DirectorySearcher dssearch = new DirectorySearcher(entry);
                    dssearch.Filter = "(sAMAccountName=" + UserName + ")";
                    dssearch.PropertiesToLoad.Add("cn");
                    SearchResult sresult = dssearch.FindOne();
                    string path = sresult.Path;
                    DirectoryEntry dsresult = sresult.GetDirectoryEntry();
                    if (dsresult.Properties["givenName"].Value != null)
                        txtFullName.Text = dsresult.Properties["givenName"][0].ToString();
                    //if (dsresult.Properties["sn"].Value != null)
                    //    txtTelephone.Text = dsresult.Properties["sn"][0].ToString();
                    if (dsresult.Properties["mail"].Value != null)
                        txtEmail.Text = dsresult.Properties["mail"][0].ToString();
                    if (dsresult.Properties["telephoneNumber"].Value != null)
                        txtTelephone.Text = dsresult.Properties["telephoneNumber"][0].ToString();
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message.ToString();
                }
            }


After hosting this, username is coming  ASPNET.  and able to connect AD.

Please have a look on this and help me..
Thanks in advance
Jidhu
Robbe Morris replied to jidheesh Rajan on 12-Dec-12 08:31 AM
I already gave you the code but yet you are not using it.  I can't help you if you won't help yourself.