从 SQL 用户 ID 获取 Active Directory 名称
Get Active Directory Name From SQL User ID
我有一个 SQL table,其中包含活动目录 ID,但没有名称。所以,问题的核心是我需要找到关联的名称。我试图只使用 SQL 来查询活动目录,但我遇到了 运行 的问题,所以我的下一次尝试是使用 C#.NET 并显示这些 ID 及其关联的活动目录 "givenname" 在页面上。
我目前正在尝试使用 Gridview 来执行此操作。
下面的工作代表我尝试在 ID 列旁边创建一个 gridview 列,并用相关的用户名填充它。
现在返回的是第一列中的用户名,一遍又一遍地重复。
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("spIDsSelect", dbConnection);
adapter.Fill(dt);
gvLookup.DataSource = dt;
gvLookup.DataBind();
}
string connection = "LDAP://....";
using (DirectoryEntry DE = new DirectoryEntry(connection))
{
DirectorySearcher dssearch = new DirectorySearcher(connection);
foreach (DataRow dr in dt.Rows)
{
var name = dt.Rows[0][0].ToString();
dssearch.Filter = String.Format("(&(objectCategory=user)(samaccountname={0}))", name);
foreach (SearchResult sresult in dssearch.FindAll())
{
DirectoryEntry de = sresult.GetDirectoryEntry();
{
foreach (GridViewRow row in gvLookup.Rows)
{
((Label)row.FindControl("lbName")).Text = de.Properties["givenname"][0].ToString();
}
}
}
}
}
<asp:GridView ID="gvLookup" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="Lookup" CellPadding="4" ForeColor="#333333" GridLines="None" HorizontalAlign="Left"
DataKeyNames="recID" >
<AlternatingRowStyle BackColor="White"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Name" >
<ItemTemplate>
<asp:Label runat="server" ID="lbName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
从数据库中检索数据后,只需在数据表中添加一列即可:
dt.Columns.Add("ActiveDirectoryName", typeof(string));
这行得通。无需遍历所有数据行,只需遍历 Gridview。我希望每个 ID 只有一个结果,并且可以使用目录条目名称填充我的 Gridview 中的标签。检查以确保 sresult 不为空很重要,因为 table 中的某些用户可能不再有效,这会提示对象引用错误。
string connection = "LDAP...";
using (DirectoryEntry DE = new DirectoryEntry(connection))
{
DirectorySearcher dssearch = new DirectorySearcher(connection);
{
var sgID = ((Label)gr.FindControl("lbID")).Text;
dssearch.Filter = String.Format("(&(objectCategory=user)(samaccountname={0}))", ID);
SearchResult sresult = dssearch.FindOne();
if (sresult != null)
{
DirectoryEntry de = sresult.GetDirectoryEntry();
((Label)gr.FindControl("lbName")).Text = de.Name;
}
}
}
我有一个 SQL table,其中包含活动目录 ID,但没有名称。所以,问题的核心是我需要找到关联的名称。我试图只使用 SQL 来查询活动目录,但我遇到了 运行 的问题,所以我的下一次尝试是使用 C#.NET 并显示这些 ID 及其关联的活动目录 "givenname" 在页面上。
我目前正在尝试使用 Gridview 来执行此操作。
下面的工作代表我尝试在 ID 列旁边创建一个 gridview 列,并用相关的用户名填充它。
现在返回的是第一列中的用户名,一遍又一遍地重复。
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("spIDsSelect", dbConnection);
adapter.Fill(dt);
gvLookup.DataSource = dt;
gvLookup.DataBind();
}
string connection = "LDAP://....";
using (DirectoryEntry DE = new DirectoryEntry(connection))
{
DirectorySearcher dssearch = new DirectorySearcher(connection);
foreach (DataRow dr in dt.Rows)
{
var name = dt.Rows[0][0].ToString();
dssearch.Filter = String.Format("(&(objectCategory=user)(samaccountname={0}))", name);
foreach (SearchResult sresult in dssearch.FindAll())
{
DirectoryEntry de = sresult.GetDirectoryEntry();
{
foreach (GridViewRow row in gvLookup.Rows)
{
((Label)row.FindControl("lbName")).Text = de.Properties["givenname"][0].ToString();
}
}
}
}
}
<asp:GridView ID="gvLookup" runat="server" AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="Lookup" CellPadding="4" ForeColor="#333333" GridLines="None" HorizontalAlign="Left"
DataKeyNames="recID" >
<AlternatingRowStyle BackColor="White"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Name" >
<ItemTemplate>
<asp:Label runat="server" ID="lbName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
从数据库中检索数据后,只需在数据表中添加一列即可:
dt.Columns.Add("ActiveDirectoryName", typeof(string));
这行得通。无需遍历所有数据行,只需遍历 Gridview。我希望每个 ID 只有一个结果,并且可以使用目录条目名称填充我的 Gridview 中的标签。检查以确保 sresult 不为空很重要,因为 table 中的某些用户可能不再有效,这会提示对象引用错误。
string connection = "LDAP...";
using (DirectoryEntry DE = new DirectoryEntry(connection))
{
DirectorySearcher dssearch = new DirectorySearcher(connection);
{
var sgID = ((Label)gr.FindControl("lbID")).Text;
dssearch.Filter = String.Format("(&(objectCategory=user)(samaccountname={0}))", ID);
SearchResult sresult = dssearch.FindOne();
if (sresult != null)
{
DirectoryEntry de = sresult.GetDirectoryEntry();
((Label)gr.FindControl("lbName")).Text = de.Name;
}
}
}