Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. //Authenticate a User Against the Directory
  2. private bool Authenticate(string userName,string password, string domain)
  3. {
  4.  
  5. if (userName == "" || password == "")
  6. {
  7. return false;
  8. }
  9.  
  10. bool authentic = false;
  11. try
  12. {
  13. DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,userName, password);
  14. object nativeObject = entry.NativeObject;
  15. authentic = true;
  16. }
  17. catch (DirectoryServicesCOMException) { }
  18. return authentic;
  19. }
  20.  
  21. private bool Authenticate(string userName,string password, string domain, string group)
  22.  
  23. bool IsInGroup(string user, string group)
  24. {
  25. using (var identity = new WindowsIdentity(user))
  26. {
  27. var principal = new WindowsPrincipal(identity);
  28. return principal.IsInRole(group);
  29. }
  30. }
  31.  
  32. public bool AuthenticateGroup(string userName, string password, string domain, string group)
  33. {
  34.  
  35.  
  36. if (userName == "" || password == "")
  37. {
  38. return false;
  39. }
  40.  
  41. try
  42. {
  43. DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password);
  44. DirectorySearcher mySearcher = new DirectorySearcher(entry);
  45. mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";
  46. SearchResult result = mySearcher.FindOne();
  47.  
  48. foreach (string GroupPath in result.Properties["memberOf"])
  49. {
  50. if (GroupPath.Contains(group))
  51. {
  52. return true;
  53. }
  54. }
  55. }
  56. catch (DirectoryServicesCOMException)
  57. {
  58. }
  59. return false;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement