To retrieve Active Directory user properties using the System.DirectoryServices.AccountManagement namespace in C#, you can use the UserPrincipal class.
Here's an example of how to retrieve user properties using the UserPrincipal class:
using System.DirectoryServices.AccountManagement; // Set up the context for the Active Directory domain using (var context = new PrincipalContext(ContextType.Domain, "domain.com")) { // Search for a user by username var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "username"); if (user != null) { // Retrieve the user properties var firstName = user.GivenName; var lastName = user.Surname; var email = user.EmailAddress; var phoneNumber = user.VoiceTelephoneNumber; var department = user.Department; } } In this example, we first set up a PrincipalContext object to represent the Active Directory domain. We then call the UserPrincipal.FindByIdentity method to retrieve a UserPrincipal object representing the user with the specified username.
We then retrieve the user properties using the properties of the UserPrincipal object, such as GivenName, Surname, EmailAddress, VoiceTelephoneNumber, and Department.
By using the UserPrincipal class, you can easily retrieve Active Directory user properties in your C# application. Note that you can also modify user properties using the UserPrincipal class, by setting the properties and calling the Save method of the UserPrincipal object.
"C# Active Directory get user properties using UserPrincipal"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "username"); if (user != null) { // Access user properties string displayName = user.DisplayName; string emailAddress = user.EmailAddress; // Add more properties as needed } } "C# Active Directory get custom user properties"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "username"); if (user != null) { // Access custom user properties string customProperty = user.ExtensionGet("customProperty").ToString(); // Add more custom properties as needed } } "C# Active Directory get user properties by email"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.UserPrincipalName, "user@example.com"); if (user != null) { // Access user properties string displayName = user.DisplayName; string samAccountName = user.SamAccountName; // Add more properties as needed } } "C# Active Directory get user properties by GUID"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.Guid, new Guid("userGuid")); if (user != null) { // Access user properties string displayName = user.DisplayName; string samAccountName = user.SamAccountName; // Add more properties as needed } } "C# Active Directory get user properties by SID"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.Sid, "userSid"); if (user != null) { // Access user properties string displayName = user.DisplayName; string samAccountName = user.SamAccountName; // Add more properties as needed } } "C# Active Directory get user properties by EmployeeId"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { PrincipalSearcher searcher = new PrincipalSearcher(new UserPrincipal(context) { EmployeeId = "employeeId" }); UserPrincipal user = searcher.FindOne() as UserPrincipal; if (user != null) { // Access user properties string displayName = user.DisplayName; string samAccountName = user.SamAccountName; // Add more properties as needed } } "C# Active Directory get user properties with wildcard search"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { PrincipalSearcher searcher = new PrincipalSearcher(new UserPrincipal(context) { SamAccountName = "user*" }); UserPrincipal user = searcher.FindOne() as UserPrincipal; if (user != null) { // Access user properties string displayName = user.DisplayName; string emailAddress = user.EmailAddress; // Add more properties as needed } } "C# Active Directory get all user properties"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "username", UserPrincipal.GetAuthorizationGroups(context)); if (user != null) { // Access all user properties foreach (var property in user.Properties.PropertyNames) { Console.WriteLine($"{property}: {user.Properties[property][0]}"); } } } "C# Active Directory get user properties with specific attributes"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "username"); if (user != null) { // Access specific user properties string displayName = user.DisplayName; string emailAddress = user.EmailAddress; // Add more specific properties as needed } } "C# Active Directory get user properties with LastLogonDate"
UserPrincipal in C#.// Code Implementation using System.DirectoryServices.AccountManagement; using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) { UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "username"); if (user != null) { // Access user properties, including LastLogonDate string displayName = user.DisplayName; string emailAddress = user.EmailAddress; DateTime? lastLogonDate = user.LastLogon; // Add more properties as needed } } package sed camera-calibration observablecollection react-cookie pgp cookies kubectl coupon jss