Skip to content

Instantly share code, notes, and snippets.

@cpoDesign
Last active March 11, 2016 21:03
Show Gist options
  • Save cpoDesign/d870f96b78a3ab8dfa32 to your computer and use it in GitHub Desktop.
Save cpoDesign/d870f96b78a3ab8dfa32 to your computer and use it in GitHub Desktop.
Get User details from active directory
using (var context = new PrincipalContext(ContextType.Domain, "spacenation"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll().Where(x => x.SamAccountName.Contains("UserName")))
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
Console.WriteLine("=======================================");
var auth = result as AuthenticablePrincipal;
if (auth != null)
{
Console.WriteLine("Name: " + auth.Name);
Console.WriteLine("Last Logon Time: " + auth.LastLogon);
Console.WriteLine();
}
Console.WriteLine("lastLogon: " + de.Properties["lastLogon"]);
foreach (string name in de.Properties.PropertyNames)
{
Console.WriteLine("{0}: {1}", name, de.Properties[name].Value);
}
Console.WriteLine();
}
}
}
var ad = new AdUserInfo();
using (var context = new PrincipalContext(ContextType.Domain, domain))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
{
ad.FirstName = user.GivenName;
ad.LastName = user.Surname;
ad.Email = user.EmailAddress;
ad.SamAccountName = user.SamAccountName;
ad.LastLogonDate = user.LastLogon;
DirectoryEntry de = user.GetUnderlyingObject() as DirectoryEntry;
object value = de.Properties["title"].Value;
ad.Title = value != null ? value.ToString() : string.Empty;
if (includeExtendedData)
{
ad.Properties = new List<KeyValuePair<string, object>>();
foreach (string propertyName in de.Properties.PropertyNames)
{
var keyValuePair = new KeyValuePair<string, object>(propertyName, de.Properties[propertyName].Value);
ad.Properties.Add(keyValuePair);
}
}
}
}
return ad;
First Name: Dummy
Last Name : User
User principal name: UserName@SpaceNation.com
=======================================
Name: Dummy User
Last Logon Time: 03/03/2016 09:23:56
lastLogon: System.DirectoryServices.PropertyValueCollection
objectClass: System.Object[]
cn: Dummy User
sn: User
givenName: Dummy
distinguishedName: CN=Dummy User,OU=3rd party services,OU=Services,DC=
SpaceNation,DC=dc
instanceType: 4
whenCreated: 29/02/2016 00:03:22
whenChanged: 09/03/2016 22:52:05
displayName: Dummy User
uSNCreated: System.__ComObject
uSNChanged: System.__ComObject
nTSecurityDescriptor: System.__ComObject
name: Dummy User
objectGUID: System.Byte[]
userAccountControl: 66048
badPwdCount: 0
codePage: 0
countryCode: 0
badPasswordTime: System.__ComObject
lastLogoff: System.__ComObject
lastLogon: System.__ComObject
pwdLastSet: System.__ComObject
primaryGroupID: 513
userParameters: P☺CtxCfgPresent????☺CtxCfgFlags1????☺CtxShadow????*☻☺CtxMinEncryptionLevel?
objectSid: System.Byte[]
accountExpires: System.__ComObject
logonCount: 6
sAMAccountName: UserName
sAMAccountType: 805306368
userPrincipalName: UserName@SpaceNation.dc
lockoutTime: System.__ComObject
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=SpaceNation,DC=dc
dSCorePropagationData: System.Object[]
lastLogonTimestamp: System.__ComObject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment