ASP.NET Active Directory C#字段规范
我们这里有一个活动目录。提供用户的唯一用户id后,我需要访问与该用户id相关的organization->manager->name属性。基本上,这将用于向提交请求的人的经理发送审批表 知道如何实现吗?使用该类读取用户对象的相应属性。ASP.NET Active Directory C#字段规范,c#,.net,asp.net,active-directory,C#,.net,Asp.net,Active Directory,我们这里有一个活动目录。提供用户的唯一用户id后,我需要访问与该用户id相关的organization->manager->name属性。基本上,这将用于向提交请求的人的经理发送审批表 知道如何实现吗?使用该类读取用户对象的相应属性。DirectoryEntry的构造函数要求您具有用户的LDAP路径。获取LDAP路径通常会很棘手,因为IIS更喜欢只传递SAM帐户名。如果您提供更多关于您拥有的用户id的详细信息,则更容易为您指明正确的方向 要做到这一点,运行ASP.NET应用程序的帐户需要对广告具
DirectoryEntry
的构造函数要求您具有用户的LDAP路径。获取LDAP路径通常会很棘手,因为IIS更喜欢只传递SAM帐户名。如果您提供更多关于您拥有的用户id的详细信息,则更容易为您指明正确的方向
要做到这一点,运行ASP.NET应用程序的帐户需要对广告具有读取权限,而默认情况下可能没有。如果web服务器属于AD,将应用程序池更改为在“NetworkService”下运行是最简单的方法。ASP.NET应用程序随后将使用服务器的MACHINE$帐户访问AD。由于您使用的是.NET 3.5及更高版本,您应该检查
System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。请在此处阅读所有相关内容:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// do something here....
}
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
// if found....
if (group != null)
{
// iterate over members
foreach (Principal p in group.GetMembers())
{
Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
// do whatever you need to do to those members
}
}
新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易
我不是100%确定在你的具体案例中你想做什么。。。
UserPrincipal
有一个EmployeeId
属性-这就是您要搜索的内容吗?您可以使用以下代码:
/* Retreiving object from SID
*/
string SidLDAPURLForm = "LDAP://WM2008R2ENT:389/<SID={0}>";
System.Security.Principal.SecurityIdentifier sidToFind = new System.Security.Principal.SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106");
/*
System.Security.Principal.NTAccount user = new System.Security.Principal.NTAccount("SomeUsername");
System.Security.Principal.SecurityIdentifier sidToFind = user.Translate(System.Security.Principal.SecurityIdentifier)
*/
DirectoryEntry userEntry = new DirectoryEntry(string.Format(SidLDAPURLForm, sidToFind.Value));
string managerDn = userEntry.Properties["manager"].Value.ToString();
/*从SID检索对象
*/
字符串SidLDAPURLForm=“LDAP://WM2008R2ENT:389/”;
System.Security.Principal.SecurityIdentifier sidToFind=新系统.Security.Principal.SecurityIdentifier(“S-1-5-21-3115856885-816991240-3296679909-1106”);
/*
System.Security.Principal.NTAccount user=新的System.Security.Principal.NTAccount(“SomeUsername”);
System.Security.Principal.SecurityIdentifier sidToFind=user.Translate(System.Security.Principal.SecurityIdentifier)
*/
DirectoryEntry userEntry=新的DirectoryEntry(string.Format(SidLDAPURLForm,sidToFind.Value));
字符串managerDn=userEntry.Properties[“manager”].Value.ToString();
但是,您也可以找到其他方法来搜索绑定到Active directory 这似乎是我需要做的,但很抱歉,对文字值不太确定..无论如何,你可以澄清一下吗?我编辑以添加到注释中,以从名称恢复SID。不是,实际上不是。这里有两个唯一的id,一个是数字,另一个是字母数字。我需要后者。所以基本上会输入一个像hah2jah1这样的ID,每个员工都有一个部门负责人,所以我需要根据ID查找该负责人。