C# Application Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use the WindowsIdentity
class to get the current Windows identity, and then the DirectoryEntry
class to query Active Directory for information about the user. Here is an example:
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(); DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://" + currentIdentity.Name); string currentUser = directoryEntry.Properties["sAMAccountName"].Value.ToString(); Console.WriteLine("Current user: " + currentUser);
You can use the DirectorySearcher
class to search for the user in Active Directory, and then the DirectoryEntry
class to retrieve the groups that the user is a member of. Here is an example:
string userName = "jdoe"; DirectorySearcher searcher = new DirectorySearcher("(samaccountname=" + userName + ")"); SearchResult result = searcher.FindOne(); if (result != null) { DirectoryEntry directoryEntry = result.GetDirectoryEntry(); PropertyValueCollection groups = directoryEntry.Properties["memberOf"]; foreach (string group in groups) { Console.WriteLine(group); } }
You can use the DirectoryEntry
class to bind to Active Directory with the username and password, and check if the binding was successful. Here is an example:
string userName = "jdoe"; string password = "password123"; DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://mydomain.com", userName, password); try { object obj = directoryEntry.NativeObject; Console.WriteLine("Username and password are valid."); } catch (DirectoryServicesCOMException) { Console.WriteLine("Username and password are invalid."); }
Note that the NativeObject
property is used to force a validation of the credentials. If the username and password are invalid, this will throw a DirectoryServicesCOMException
.
C# Active Directory Authentication Example:
Active Directory (AD) authentication in C# involves connecting to an AD server and verifying user credentials.
using System.DirectoryServices.AccountManagement; class Program { static void Main() { using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "yourdomain.com")) { bool isValid = context.ValidateCredentials("username", "password"); Console.WriteLine($"Is Valid: {isValid}"); } } }
Authenticate User Against Active Directory in C#:
Validate a user's credentials against Active Directory.
using System.DirectoryServices.AccountManagement; bool AuthenticateUser(string username, string password) { using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "yourdomain.com")) { return context.ValidateCredentials(username, password); } }
Validate Active Directory Credentials in C#:
Check if provided credentials are valid in Active Directory.
bool isValid = AuthenticateUser("username", "password"); Console.WriteLine($"Credentials are valid: {isValid}");
C# Check Username and Password Against AD:
A simple function to check if a username and password are valid.
bool isValid = AuthenticateUser("username", "password"); Console.WriteLine($"Credentials are valid: {isValid}");
LDAP Authentication in C#:
Use the Lightweight Directory Access Protocol (LDAP) for authentication.
using System.DirectoryServices; bool AuthenticateLDAP(string username, string password) { DirectoryEntry entry = new DirectoryEntry("LDAP://yourdomain.com", username, password); try { // Attempt to bind with the provided credentials object nativeObject = entry.NativeObject; return true; } catch (DirectoryServicesCOMException) { return false; } }
Windows Authentication in C# Against Active Directory:
Leverage Windows authentication for seamless AD integration.
using System.Web.Security; bool isWindowsAuthenticated = HttpContext.Current.Request.LogonUserIdentity.IsAuthenticated;
C# Login Form Using Active Directory:
Create a login form that authenticates users against Active Directory.
string username = "userInput"; string password = "passwordInput"; bool isValid = AuthenticateUser(username, password); Console.WriteLine($"Login successful: {isValid}");
C# Active Directory Login Code Example:
A complete example of a login function using Active Directory.
bool loginSuccess = AuthenticateUser("username", "password"); Console.WriteLine($"Login success: {loginSuccess}");
LDAP Bind in C# for User Authentication:
Perform an LDAP bind operation for user authentication.
using System.DirectoryServices.Protocols; bool AuthenticateLDAPBind(string username, string password) { using (LdapConnection connection = new LdapConnection("yourldapserver")) { connection.Bind(new NetworkCredential(username, password)); return true; // If bind succeeds } }
C# Check If User Exists in Active Directory:
Verify if a user exists in Active Directory.
bool userExists = CheckUserExists("username"); Console.WriteLine($"User exists: {userExists}");