How to get the current user’s Active Directory details in C#

The “pre Windows 2000” name i.e. DOMAIN\SomeBody, the Somebody portion is known as sAMAccountName. So try: using(DirectoryEntry de = new DirectoryEntry(“LDAP://MyDomainController”)) { using(DirectorySearcher adSearch = new DirectorySearcher(de)) { adSearch.Filter = “(sAMAccountName=someuser)”; SearchResult adSearchResult = adSearch.FindOne(); } } [email protected] is the UserPrincipalName, but it isn’t a required field.

See if user is part of Active Directory group in C# + Asp.net

With 3.5 and System.DirectoryServices.AccountManagement this is a bit cleaner: public List<string> GetGroupNames(string userName) { var pc = new PrincipalContext(ContextType.Domain); var src = UserPrincipal.FindByIdentity(pc, userName).GetGroups(pc); var result = new List<string>(); src.ToList().ForEach(sr => result.Add(sr.SamAccountName)); return result; }

How to effectively use the `-Filter` parameter on Active Directory cmdlets?

Note about Azure AD cmdlets This answer is crafted around the Active Directory cmdlets installed and available from Remote Server Administration Tools (RSAT). However, the Azure AD cmdlets make use of Microsoft Graph (OData v4.0 specification) to run queries against Azure AD while the RSAT cmdlets[1] rely on an implementation of the PowerShell Expression Engine … Read more

Querying Active Directory from SQL Server 2005

Pretty general question but here are some pointers. You need a linked server creating on the SQL Server that points to ADSI (Active Directory Service Interface) something like this will do it. EXEC sp_addlinkedserver ‘ADSI’, ‘Active Directory Services 2.5’, ‘ADSDSOObject’, ‘adsdatasource’ Then you can use the following sort of query. SELECT * FROM OPENQUERY(ADSI, ‘SELECT … Read more

How do I validate Active Directory creds over LDAP + SSL?

I was able to validate credentials using the System.DirectoryServices.Protocols namespace, thanks to a co-worker. Here’s the code: // See http://support.microsoft.com/kb/218185 for full list of LDAP error codes const int ldapErrorInvalidCredentials = 0x31; const string server = “sd.example.com:636”; const string domain = “sd.example.com”; try { using (var ldapConnection = new LdapConnection(server)) { var networkCredential = new … Read more