(Using System.DirectoryServices.AccountManagement)
Before .Net managing Active Directory objects is a bit lengthy and you need a good knowledge on the principal store to have your head around on what you want to do. We ususally use the System.DirectoryServices namespace but with .Net 3.5 they introduced System.DirectoryServices.AccountManagement which is manages directory objects independent of the System.DirectoryServices namespace. So what are the advantages of using this if I have already a library created for the whole AD Methods that System.DirectoryServices exposed? Because everything is really simple in terms of managing a user, computer or group principal and performing queries on the stores are much faster thanks to the Fast Concurrent Bind (FSB) feature which caches the connection which decreases the number of ports used in the process. I remember I had posted a while back Active Directory Objects and C# which is basically everything regarding AD Methods in terms of Users and Group management and if you see the codebase is a bit lengthy and you need a bit of understanding on setting and getting hex values thats why I ennumerated it. Now I had rewritten it using the System.DirectoryServices.AccountManagement namespace, fucntionalities remain the same but its easier to understand and there are fewer lines.| The code is divided into several regions but here are the 5 key regions with their methods explained Validate Methods
Search Methods
User Account Methods
Group Methods
Helper Methods
Now here are the codes using System; using System.Collections; using System.Text; using System.DirectoryServices.AccountManagement; using System.Data; using System.Configuration; public class ADMethodsAccountManagement { #region Variables private string sDomain = "test.com"; private string sDefaultOU = "OU=Test Users,OU=Test,DC=test,DC=com"; private string sDefaultRootOU = "DC=test,DC=com"; private string sServiceUser = @"ServiceUser"; private string sServicePassword = "ServicePassword"; #endregion #region Validate Methods /// <summary> /// Validates the username and password of a given user /// </summary> /// <param name="sUserName">The username to validate</param> /// <param name="sPassword">The password of the username to validate</param> /// <returns>Returns True of user is valid</returns> public bool ValidateCredentials(string sUserName, string sPassword) { PrincipalContext oPrincipalContext = GetPrincipalContext(); return oPrincipalContext.ValidateCredentials(sUserName, sPassword); } /// <summary> /// Checks if the User Account is Expired /// </summary> /// <param name="sUserName">The username to check</param> /// <returns>Returns true if Expired</returns> public bool IsUserExpired(string sUserName) { UserPrincipal oUserPrincipal = GetUser(sUserName); if (oUserPrincipal.AccountExpirationDate != null) { return false; } else { return true; } } /// <summary> /// Checks if user exsists on AD /// </summary> /// <param name="sUserName">The username to check</param> /// <returns>Returns true if username Exists</returns> public bool IsUserExisiting(string sUserName) { if (GetUser(sUserName) == null) { return false; } else { return true; } } /// \<summary\> /// Checks if user accoung is locked /// \</summary\> /// \<param name="sUserName"\>The username to check\</param\> /// \<returns\>Retruns true of Account is locked\</returns\> public bool IsAccountLocked(string sUserName) { UserPrincipal oUserPrincipal = GetUser(sUserName); return oUserPrincipal.IsAccountLockedOut(); } #endregion |
手机版|BC Morning Website ( Best Deal Inc. 001 )
GMT-8, 2025-7-8 11:37 , Processed in 0.017740 second(s), 16 queries .
Supported by Best Deal Online X3.5
© 2001-2025 Discuz! Team.