Guest User

Untitled

a guest
May 2nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. public class AD
  2. {
  3. // Fields
  4. private static string ADPassword = ConfigurationManager.AppSettings["ADPassword"].ToString();
  5. private static string ADPath = ConfigurationManager.AppSettings["ADConnection"].ToString();
  6. private static string ADServerName = ConfigurationManager.AppSettings["ADServerName"].ToString();
  7. private static string ADUserName = ConfigurationManager.AppSettings["ADUserName"].ToString();
  8.  
  9. // Methods
  10. public static string GetLogin(string sUserName, string sPassword)
  11. {
  12. try
  13. {
  14. DirectoryEntry entry = new DirectoryEntry(ADPath, ADServerName + sUserName, sPassword);
  15. object nativeObject = entry.NativeObject;
  16. return string.Empty;
  17. }
  18. catch
  19. {
  20. return "Invalid Username or Password";
  21. }
  22. }
  23.  
  24. public static string Update(string sUserName, string sOldPassword, string sNewPassword)
  25. {
  26. string message;
  27. try
  28. {
  29. DirectoryEntry searchRoot = new DirectoryEntry();
  30. searchRoot.Path = ADPath;
  31. searchRoot.Username = ADServerName + ADUserName;
  32. searchRoot.Password = ADPassword;
  33. DirectorySearcher searcher = new DirectorySearcher(searchRoot);
  34. searcher.Filter = "(SAMAccountName=" + sUserName + ")";
  35. DirectoryEntry directoryEntry = searcher.FindOne().GetDirectoryEntry();
  36. directoryEntry.Invoke("ChangePassword", new object[] { sOldPassword, sNewPassword });
  37. directoryEntry.CommitChanges();
  38. directoryEntry.Close();
  39. message = string.Empty;
  40. }
  41. catch (Exception exception)
  42. {
  43. try
  44. {
  45. message = exception.InnerException.Message;
  46. }
  47. catch
  48. {
  49. message = exception.Message;
  50. }
  51. }
  52. return message;
  53. }
  54. }
  55.  
  56. public bool UserExists(string userName)
  57. {
  58. DirectoryEntry searchRoot = new DirectoryEntry("LDAP://dc=yourcompany,dc=com", userName, password);
  59. DirectorySearcher searchForUser = new DirectorySearcher(searchRoot);
  60.  
  61. searchForUser.SearchScope = SearchScope.SubTree;
  62. searchForUser.Filter = string.Format("(&(objectCategory=Person)(anr={0}))", userName);
  63.  
  64. if(searchForUser.FindOne() != null)
  65. {
  66. return true;
  67. }
  68. else
  69. {
  70. return false;
  71. }
  72. }
  73.  
  74. LDAP://dc=yourcompany,dc=com
  75.  
  76. LDAP://cn=Users,dc=yourcompany,dc=com
  77.  
  78. PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");
  79.  
  80. UserPrincipal foundUser = UserPrincipal.FindByIdentity(ctx, "your user name");
Add Comment
Please, Sign In to add comment