LDAP manager attribute

I am creating an LDAP class that contains a function that returns the username of the manager of the current user.

I know that I can use the "manager" attribute to return CN = "name", OU = "group", DC = "company", etc.

I specifically want the managers username, does anyone know if there is an attribute string that I can send to LDAP that specifically gets the managers-only username? If not, is there an alternative method?

+3
source share
4 answers

Now I decided to solve this problem.

Basically, an attribute managerin LDAP returns the distinguishedNamemaanger user attribute.

, LDAP , distinguishedName, , .

+1

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

- : , .NET 3.5, System.DirectoryServices, System.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);
+1

. , .

, , null - . , .

, , 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
{ }
0

Source: https://habr.com/ru/post/1787897/


All Articles