【问题标题】:LDAP Manager AttributeLDAP 管理器属性
【发布时间】:2011-06-15 17:02:06
【问题描述】:

我正在创建一个 LDAP 类,其中包含一个返回当前用户的管理员用户名的函数。

我知道我可以使用“manager”属性返回 CN="name"、OU="group"、DC="company" 等。

我特别想要经理用户名,有人知道我是否可以发送到 LDAP 的属性字符串专门获取经理用户名吗?如果没有,是否有其他方法可以做到这一点?

【问题讨论】:

    标签: c# asp.net vb.net webforms ldap


    【解决方案1】:

    两个示例都可以正常工作。但我相信还有改进的空间。

    方法不应该检查参数是否为空,返回空是一个坏习惯。另一方面,抛出异常更合适。

    应该可以从提供的UserPrincipal 中检索上下文,而不是询问上下文,因此查找当前用户应该没有任何问题。

    public static UserPrincipal GetManager(UserPrincipal user)
    {
      var userEntry = user.GetUnderlyingObject() as DirectoryEntry;
      if (userEntry.Properties["manager"] != null
        && userEntry.Properties["manager"].Count > 0)
      {
        string managerDN = userEntry.Properties["manager"][0].ToString();
        return UserPrincipal.FindByIdentity(user.Context,managerDN);
      }
      else
        throw new UserHasNoManagerException();
    }
    
    class UserHasNoManagerException : Exception
    { }
    

    【讨论】:

      【解决方案2】:

      上面的 GetManager 工作得很好,除非没有设置管理器。稍作改动以适应这种情况:

      // return manager for a given user
      public UserPrincipal GetManager(PrincipalContext ctx, UserPrincipal user) {
          if (user != null) {
              // get the DirectoryEntry behind the UserPrincipal object
              var dirEntryForUser = user.GetUnderlyingObject() as DirectoryEntry;
      
              if (dirEntryForUser != null) {
                  // check to see if we have a manager name - if so, grab it
                  if (dirEntryForUser.Properties["manager"] != null && dirEntryForUser.Properties["manager"].Count > 0) {
                      string managerDN = dirEntryForUser.Properties["manager"][0].ToString();
                      // find the manager UserPrincipal via the managerDN 
                      return UserPrincipal.FindByIdentity(ctx, managerDN);
                  }
              }
          }
          return null;
      }
      

      【讨论】:

        【解决方案3】:

        我现在已经想出了解决办法。

        基本上,LDAP 中的 manager 属性会检索 maanger 用户的 distinguishedName 属性。

        因此,如果我在 LDAP 中搜索包含从经理返回的 distinguishedName 的用户,那么我可以通过这种方式获取他们的任何属性。

        【讨论】:

          【解决方案4】:

          一种可能的解决方案是使用类似的方法 - 这要求您使用 .NET 3.5 并且您已经引用了 System.DirectoryServicesSystem.DirectoryServices.AccountManagement

          // return manager for a given user
          public UserPrincipal GetManager(PrincipalContext ctx, UserPrincipal user)
          {
              UserPrincipal result = null;
          
              if (user != null)
              {
                  // get the DirectoryEntry behind the UserPrincipal object
                  DirectoryEntry dirEntryForUser = user.GetUnderlyingObject() as DirectoryEntry;
          
                  if (dirEntryForUser != null)
                  {
                       // check to see if we have a manager name - if so, grab it
                       if (dirEntryForUser.Properties["manager"] != null)
                       {
                           string managerDN = dirEntryForUser.Properties["manager"][0].ToString();
          
                           // find the manager UserPrincipal via the managerDN 
                           result = UserPrincipal.FindByIdentity(ctx, managerDN);
                       }
                  }
              }
          
              return result;
          }
          

          然后您可以调用此方法,例如像这样:

          // Create default domain context
          PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
          
          // find yourself - you could also search for other users here
          UserPrincipal myself = UserPrincipal.Current;
          
          // get the manager for myself
          UserPrincipal myManager = GetManager(ctx, myself);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多