Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search a user login id by first name and last name

Tags:

c#

c#-4.0

I found How can I get a list of users from active directory?

It is helpful when I only have a few users, but I have so many users in AD, so when I run my query

if ((String)(entry.Properties["sn"].Value) == "lname"
     && (String)(entry.Properties["givenName"].Value) == "fname")
{
    return entry.Properties["samAccountName"].Value.ToString();
}

it took too long to finish.

What can I do to search one particular user logon id by first name and last name?

like image 792
user1225072 Avatar asked Apr 20 '12 15:04

user1225072


People also ask

How to get first name and last name in sap user Id?

The tables USER_ADDR and USER_ADDRS will help you get the first name and last name of a user Id.

How to get user full name in sap?

For example, if you want to retrieve the SAP username, use the table ADRP. The ADRP-NAME_FIRST and ADRP-NAME_LAST store the first name and the last name of the SAP user. Hence, T=the full SAP username can be found in the ADRP-NAME_TEXT.

How do I find my SAP user ID?

Answer: Your SAP User Id is typically the same as your email address without the domain address. For example – email account ([email protected]). The SAP User ID would be SmithJ. 2.


2 Answers

Since you're on .NET 4, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

  • Managing Directory Security Principals in the .NET Framework 3.5
  • MSDN docs on System.DirectoryServices.AccountManagement

Basically, you can define a domain context and easily find users and/or groups in AD:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user - by e.g. his "samAccountName", or the Windows user name or something
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   // do something here....     
   string samAccountName = user.SamAccountName;
}

If you cannot find a user specified by a user name, you can also use the new search functionality:

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) and a last name (Surname) 
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = firstName;
qbeUser.Surname = lastName;

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

The new S.DS.AM makes it really easy to play around with users and groups in AD! And just finding a single user should be relatively quick, too.

like image 184
marc_s Avatar answered Oct 19 '22 23:10

marc_s


You should be using the AD server to do the filtering. Do this by supplying an LDAP syntax filter. Also, specify only the properties you need using the propertiesToLoad argument of FindAll:

    public static SearchResultCollection FindByName(
        string domain, string firstName, string lastName, string[] properties) {
        var rootEntry = new DirectoryEntry("LDAP://" + domain);
        var filter = string.Format("(&(sn={0})(givenName={1}))", lastName, firstName);
        var searcher = new DirectorySearcher(rootEntry, filter, properties);
        return searcher.FindAll();
    }

    // Using the method:
    var result = FindByName("mydomain", "Robert", "Smith", new[] { "samAccountName" })[0];
    string uName = (string)result.Properties["samAccountName"][0];
like image 43
Joshua Honig Avatar answered Oct 19 '22 22:10

Joshua Honig