jeudi 18 juin 2015

Get Active Directory Name From SQL User ID

I have a SQL table that contains active directory IDs, but no names. So, the core of the issue is that I need to find the associated names. I have tried to just use SQL to query the active directory, but I have run into issues with that, so my next attempt is to use C#.NET and display those IDs and their associated active directory "givenname" on the page.

I am currently trying to do this using a Gridview, but I am not particular about how it is displayed.

The effort below represents my attempt to dynamically create a gridview column next to the ID column and populate it with the associated name of the user.

protected void Page_Load(object sender, EventArgs e)
    {

        //Tabs
        if (!IsPostBack)
        {

            string connection = "LDAP://....";
            using (DirectoryEntry DE = new DirectoryEntry(connection))
            {
                DataTable dt = new DataTable();
                dt = connStringClass.ExecuteDataTable("spIDsSelect", connStringClass.DB);

                DirectorySearcher dssearch = new DirectorySearcher(connection);
                dssearch.Filter = "(userID=" + gvLookup.Columns + ")";

                SearchResult sresult = dssearch.FindOne();
                if (sresult != null)
                {
                    DirectoryEntry de = sresult.GetDirectoryEntry();

                    foreach (DataRow row in gvLookup.Rows)
                    {
                        DataColumn col = new DataColumn();
                        BoundField field = new BoundField();
                        field.HeaderText = col.ColumnName;
                        field.DataField = de.Properties["givenname"][0].ToString();
                        gvLookup.Columns.Add(field);
                    }
                }
                else
                {
                    Response.Write("No results.");
                }
            }

        }
    }
}


<asp:TemplateField HeaderText="ID" SortExpression="recID" Visible="false">                                
    <ItemTemplate><asp:Label ID="lbRecID" runat="server" Text='<%# Bind("recID") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="ID" HeaderText="ID" 
    HeaderStyle-HorizontalAlign="Left" SortExpression="ID">
    <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>

Aucun commentaire:

Enregistrer un commentaire