6

I want to retrieve the login name of a user from Active Directory.

For example the name is 'Jan Van der Linden' After giving this name as parameter I must get his login name in return for example jvdlinden

Tassisto
  • 9,877
  • 28
  • 100
  • 157

5 Answers5

6

Since you're on .NET 3.5 and up, 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

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

public string GetLoginName(string userName)
{
  // set up domain context
  PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

  // find user by name
  UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);

  if(user != null)
       return user.SamAccountName;
  else
       return string.Empty;
}

The new S.DS.AM makes it really easy to play around with users and groups in AD:

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Hey marc_s, do you know why I'm getting this error? The (&(objectCategory=user)(objectClass=user)(|(userPrincipalName=)(distinguishedName=)(name=))) search filter is invalid. – Tassisto Mar 17 '11 at 15:17
1

Check this link has needed code snipple

Validate AD-LDAP USer

using (DirectoryEntry entry = new DirectoryEntry())
        {
            entry.Username = "DOMAIN\\LOGINNAME";
            entry.Password = "PASSWORD";
            DirectorySearcher searcher = new DirectorySearcher(entry);
            searcher.Filter = "(objectclass=user)";
            try
            {
                searcher.FindOne();
                {
                    //Add Your Code if user Found..
                }
            }
            catch (COMException ex)
            {
                if (ex.ErrorCode == -2147023570)
                {
                    ex.Message.ToString();
                    // Login or password is incorrect 
                }
            }
        }
Akxaya
  • 834
  • 8
  • 6
1

using .net library you can use the following code to get username or any info from active directory

using System.Management;
using System.Management.Instrumentation;
using System.Runtime.InteropServices;
using System.DirectoryServices;

ManagementObjectSearcher Usersearcher = new ManagementObjectSearcher("Select * From Win32_ComputerSystem Where (Name LIKE 'ws%' or Name LIKE 'it%')"); 
            ManagementObjectCollection Usercollection = Usersearcher.Get(); 
            string[] sep = { "\\" };
            string[] UserNameDomain = Usercollection.Cast<ManagementBaseObject>().First()["UserName"].ToString().Split(sep, StringSplitOptions.None);

i add "Select * From Win32_ComputerSystem Where (Name LIKE 'ws%' or Name LIKE 'it%')" this will get the user name by the full name

hope this could help you

Hiyasat
  • 8,601
  • 7
  • 33
  • 59
1

this actually does almost the opposite but can be a starting point to check and modify as needed:

Finding a User in Active Directory with the Login Name

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
0

Without Identity:

private string GetLogonFromDisplayName(string displayName)
{
    var search = new DirectorySearcher(string.Format("(&(displayname={0})(objectCategory=user))", displayName));
    search.PropertiesToLoad.Add("sAMAccountName");

    SearchResult result = search.FindOne();
    if (result != null)
    {
        var logonNameResults = result.Properties["sAMAccountName"];
        if (logonNameResults == null || logonNameResults.Count == 0)
        {
            return null;
        }

        return logonNameResults[0].ToString();
    }

    return null;
}
codeMonkey
  • 4,134
  • 2
  • 31
  • 50