locked
Calling active directory RRS feed

  • Question

  • User-1924204508 posted

    Hello guys. Please help me with this one. I already added this active directory settings to my web.config file. Can you please provide me a code on how to  retrieve the user of active directory who is currently log in. please. thank you.

    <add name="abLDAP" connectionString="LDAP://ABCD01.bla.local/OU=AB Users OU, DC=ab, DC=LOCAL"/> 

    Tuesday, September 15, 2009 10:33 PM

All replies

  • User-208302135 posted

    Here is a pretty good article on doing exactly what you want to do.

    http://www.15seconds.com/issue/020730.htm

    Wednesday, September 16, 2009 12:24 AM
  • User-1659704165 posted

    Hi,

    R utrying To use windows Authecation..

    <connectionStrings>
      <add name="TestDomain1ConnectionString" connectionString="LDAP://testdomain1.test.com/CN=Users,DC=testdomain1,DC=test,DC=com" />
      <add name="TestDomain2ConnectionString" connectionString="LDAP://testdomain2.test.com/CN=Users,DC=testdomain2,DC=test,DC=com" />
      <add name="TestDomain3ConnectionString" connectionString="LDAP://testdomain3.test.com/CN=Users,DC=testdomain3,DC=test,DC=com" />

    </connectionStrings>

    OR

    http://msdn.microsoft.com/en-us/library/ms998345.aspx

    Wednesday, September 16, 2009 12:25 AM
  • User-1924204508 posted

     Thanks. But I need the c# code on how to retrieve currently login users from the active directory setting I gave you a while ago.

    Wednesday, September 16, 2009 12:42 AM
  • User-1924204508 posted

     I used the code below to retrieve the current user in active directory but nothing happens.

    public static string GetFullName(string strLogin)
        {
            string str = "";
            string strDomain;
            string strName;

            // Parse the string to check if domain name is present.
            int idx = strLogin.IndexOf('\\');
            if (idx == -1)
            {
                idx = strLogin.IndexOf('@');
            }

            if (idx != -1)
            {
                strDomain = strLogin.Substring(0, idx);
                strName = strLogin.Substring(idx + 1);
            }
            else
            {
                strDomain = Environment.MachineName;
                strName = strLogin;
            }

            DirectoryEntry obDirEntry = null;
            try
            {
                obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
                System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
                object obVal = coll["FullName"].Value;
                str = obVal.ToString();
            }
            catch (Exception ex)
            {
                str = ex.Message;
            }
            return str;
        }

    Wednesday, September 16, 2009 1:02 AM
  • User-1659704165 posted

    Hi,

    DirectorySearcher adSearch = new DirectorySearcher(new DirectoryEntry("LDAP://MyDomainController"));
    adSearch.Filter = "(sAMAccountName=someuser)";
    SearchResult adSearchResult = adSearch.FindOne();


    OR

    http://stackoverflow.com/questions/637486/how-to-get-the-current-users-active-directory-details-in-c

    Chk the above link

    Wednesday, September 16, 2009 1:05 AM
  • User-1924204508 posted

    Ok. What if I want to display the current user in AD in txtbox.text?

    txtbox.Text  =

    Wednesday, September 16, 2009 3:08 AM
  • User-1924204508 posted

    Ok. What if I want to display the current user in AD in txtbox.text?

    txtbox.Text  =

    Wednesday, September 16, 2009 3:08 AM
  • User-1659704165 posted

    Hi,
    //
    string username = HttpContext.Current.Request.ServerVariables["AUTH_USER"];


    textBox.txt = username

    http://forums.asp.net/t/454124.aspx

    oR

    U may Go for WMI

    //Finding username using WMI
    http://weblogs.asp.net/ralfw/archive/2004/01/04/47388.aspx

    http://www.codeproject.com/KB/vb/Windows_Service.aspx

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d2e4d6d9-7596-4ff3-adf0-4a4548d6b49d

    Wednesday, September 16, 2009 3:29 AM
  • User-1924204508 posted

     My problem is I want to retrieve the current user login in active directory using c#. How will I do this using the LDAP shown above. Please provide me a code to display the fullname of the current user in active directory. To test the code if correct, I want to display the current user in AD using txtbox.text. Please. thanks.

    Wednesday, September 16, 2009 3:50 AM
  • User-1924204508 posted

     I already display the current user from active directory in textbox. But the format is xxx/username.. Could you please help me revise this on how to retrive the xxx/username as FULLNAME like surname,firstname

    protected void Page_Load(object sender, EventArgs e)

    {

    string principal = this.Context.User.Identity.Name;

    string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", principal);

    string domain = "DOMAIN"; string[] properties = new string[] { "fullname" };

    DirectoryEntry adRoot = new DirectoryEntry("LDAP://xxx/OU=xxx Users OU, DC=xxx, DC=LOCAL" + domain, null, null, AuthenticationTypes.Secure);

    DirectorySearcher searcher = new DirectorySearcher(adRoot);

    txtboxLName.Text = principal;

    }

     

     

     

     

    Wednesday, September 16, 2009 4:11 AM