Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AD
- {
- // Fields
- private static string ADPassword = ConfigurationManager.AppSettings["ADPassword"].ToString();
- private static string ADPath = ConfigurationManager.AppSettings["ADConnection"].ToString();
- private static string ADServerName = ConfigurationManager.AppSettings["ADServerName"].ToString();
- private static string ADUserName = ConfigurationManager.AppSettings["ADUserName"].ToString();
- // Methods
- public static string GetLogin(string sUserName, string sPassword)
- {
- try
- {
- DirectoryEntry entry = new DirectoryEntry(ADPath, ADServerName + sUserName, sPassword);
- object nativeObject = entry.NativeObject;
- return string.Empty;
- }
- catch
- {
- return "Invalid Username or Password";
- }
- }
- public static string Update(string sUserName, string sOldPassword, string sNewPassword)
- {
- string message;
- try
- {
- DirectoryEntry searchRoot = new DirectoryEntry();
- searchRoot.Path = ADPath;
- searchRoot.Username = ADServerName + ADUserName;
- searchRoot.Password = ADPassword;
- DirectorySearcher searcher = new DirectorySearcher(searchRoot);
- searcher.Filter = "(SAMAccountName=" + sUserName + ")";
- DirectoryEntry directoryEntry = searcher.FindOne().GetDirectoryEntry();
- directoryEntry.Invoke("ChangePassword", new object[] { sOldPassword, sNewPassword });
- directoryEntry.CommitChanges();
- directoryEntry.Close();
- message = string.Empty;
- }
- catch (Exception exception)
- {
- try
- {
- message = exception.InnerException.Message;
- }
- catch
- {
- message = exception.Message;
- }
- }
- return message;
- }
- }
- public bool UserExists(string userName)
- {
- DirectoryEntry searchRoot = new DirectoryEntry("LDAP://dc=yourcompany,dc=com", userName, password);
- DirectorySearcher searchForUser = new DirectorySearcher(searchRoot);
- searchForUser.SearchScope = SearchScope.SubTree;
- searchForUser.Filter = string.Format("(&(objectCategory=Person)(anr={0}))", userName);
- if(searchForUser.FindOne() != null)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- LDAP://dc=yourcompany,dc=com
- LDAP://cn=Users,dc=yourcompany,dc=com
- PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");
- UserPrincipal foundUser = UserPrincipal.FindByIdentity(ctx, "your user name");
Add Comment
Please, Sign In to add comment