【问题标题】:ASP.NET Active Directory C# field specificationASP.NET Active Directory C# 字段规范
【发布时间】:2011-09-25 07:55:05
【问题描述】:

我们这里有一个活动目录。提供用户的唯一用户 ID,我需要访问与该用户 ID 相关的组织->经理->名称属性。基本上,这将用于向提交请求的人的经理发送批准表格。

知道如何做到这一点吗?

【问题讨论】:

    标签: c# .net asp.net active-directory


    【解决方案1】:

    您可以使用以下代码:

    /* 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(); 
    

    但您也可以找到in this post 其他方法来搜索绑定到 Active-directory。

    【讨论】:

    • 这似乎是我需要做的,但很抱歉对字面值不太确定......无论如何你可以澄清一下吗?
    • 我在评论中添加了从名称中恢复 SID 的方法。
    【解决方案2】:

    由于您使用的是 .NET 3.5 及更高版本,因此您应该查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

    // 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 让在 AD 中与用户和组一起玩变得非常容易!

    我不是 100% 确定您想在具体情况下做什么...UserPrincipal 有一个 EmployeeId 属性 - 这是您要搜索的内容吗?

    【讨论】:

    • 并非如此。这里有两个唯一的 ID,一个是数字,另一个是字母数字。我需要后一个。所以基本上会输入一个像hah2jah1这样的ID,每个员工都有一个部门负责人,所以我需要根据ID找到那个负责人。
    【解决方案3】:

    使用System.DirectoryServices.DirectoryEntry 类读出用户对象的适当属性。 DirectoryEntry 的构造函数要求您具有用户的 LDAP 路径。获取 LDAP 路径通常很棘手,因为 IIS 更喜欢仅移交 SAM 帐户名。如果您提供有关您拥有的用户 ID 的更多详细信息,则更容易为您指明正确的方向。

    为此,运行 ASP.NET 应用程序的帐户需要对 AD 的读取权限,而默认情况下可能没有此权限。如果 Web 服务器属于 AD,则将应用程序池更改为在“NetworkService”下运行是最简单的方法。然后,ASP.NET 应用程序将使用服务器的 MACHINE$ 帐户访问 AD。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      相关资源
      最近更新 更多