locked
Active Directory retrieving User Name RRS feed

  • Question

  • User13622593 posted

    I am trying to get the user name from active directory.  I used the code given but seem to get an error :Unknown error (0x80005000) on line
     using (SearchResultCollection src = ds.FindAll())


    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string adPath = "ldap://oscar:389/OU=Atlanta,DC=datadist,DC=com";
                //use current login as search key
                string sFilter = String.Format(
                    "(&(objectCategory=person)(sAMAccountName={0}))",
                    HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' })[1]
                     );
               
                //this is optional, but more efficient
                string[] attribs = new string[] { "cn", "sn", "givenName" };
                DirectoryEntry de = new DirectoryEntry(
                    adPath,
                    null,
                    null,
                    AuthenticationTypes.Secure
                    );

                using (de)
                {
                    DirectorySearcher ds = new DirectorySearcher(
                        de,
                        sFilter,
                        attribs, //set this to null to retrieve all attribs
                        SearchScope.Subtree
                        );

                    SearchResult sr = null;

                    using (SearchResultCollection src = ds.FindAll())
                    {
                        if (src.Count > 0)
                            sr = src[0];
                    }

                    if (sr == null)
                    {
                        Label1.Text = "User not found...";
                        return;
                    }

                    StringBuilder sb = new StringBuilder();

                    //dump everything we have
                    foreach (string key in sr.Properties.PropertyNames)
                    {
                        if (sr.Properties.Contains(key))
                        {
                            foreach (object o in sr.Properties[key])
                            {
                                //output every item
                                sb.AppendFormat("{0} : {1}<br>", key, o);
                            }
                        }
                    }

                    Label1.Text = "Hello " + sb.ToString();
                }
            }

        }

    Monday, January 22, 2007 12:06 PM

All replies

  • User13622593 posted

    I was able to figure this out after looking at several posts.

     In my session start of my global.asax file I have this

    string operator_id, str_ldap;

    operator_id = String.Format(HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' })[1]);

    Session.Add("SessionVariableName", operator_id);

    str_ldap = "LDAP://OSCAR";

    Session.Add("SessionLDAP", str_ldap);

     

    in the master page Page_Load event I have the following code: 

    protected void Page_Load(object sender, EventArgs e)

    {

    if (!Page.IsPostBack)

    {

    string str_userName = (string)Session["SessionVariableName"];

    string str_ldap = (string)Session["SessionLDAP"];

    DirectoryEntry m_obDirEntry = new DirectoryEntry(str_ldap);

    DirectorySearcher search = new DirectorySearcher(m_obDirEntry);

    search.Filter = String.Format("(SAMAccountName={0})", str_userName);

    search.PropertiesToLoad.Add("cn");

    SearchResultCollection results;

    results = search.FindAll();

    foreach (System.DirectoryServices.SearchResult resEnt in results)

    {

    System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();

    Label1.Text = "Hello " + (string)de.Properties["cn"][0];

    }

    }

    }

    Monday, February 12, 2007 2:53 PM