Guest User

Untitled

a guest
Jul 30th, 2018
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. How to check if user with [UserName] and [Password] is domain administrator of [DomainName] without impersonation?
  2. using (Impersonation im = new Impersonation(UserName, Domain, Password))
  3. {
  4. System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
  5. bool isDomainAdmin = identity.IsDomainAdmin(Domain, UserName, Password);
  6. if (!isDomainAdmin)
  7. {
  8. //deny access, for example
  9. }
  10. }
  11.  
  12. public static bool IsDomainAdmin(this WindowsIdentity identity, string domain, string userName, string password)
  13. {
  14. Domain d = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, domain, userName, password));
  15.  
  16. using (DirectoryEntry de = d.GetDirectoryEntry())
  17. {
  18. byte[] domainSIdArray = (byte[])de.Properties["objectSid"].Value;
  19. SecurityIdentifier domainSId = new SecurityIdentifier(domainSIdArray, 0);
  20. SecurityIdentifier domainAdminsSId = new SecurityIdentifier(WellKnownSidType.AccountDomainAdminsSid, domainSId);
  21. WindowsPrincipal wp = new WindowsPrincipal(identity);
  22. return wp.IsInRole(domainAdminsSId);
  23. }
  24. }
  25.  
  26. static void Main(string[] args) {
  27. string userDomain = "somedomain";
  28. string userName = "username";
  29. string password = "apassword";
  30.  
  31. if (IsDomainAdmin(userDomain, userName)) {
  32. string fullUserName = userDomain + @"" + userName;
  33. PrincipalContext context = new PrincipalContext(
  34. ContextType.Domain, userDomain);
  35. if (context.ValidateCredentials(fullUserName, password)) {
  36. Console.WriteLine("Success!");
  37. }
  38. }
  39. }
  40.  
  41. public static bool IsDomainAdmin(string domain, string userName) {
  42. string adminDn = GetAdminDn(domain);
  43. SearchResult result = (new DirectorySearcher(
  44. new DirectoryEntry("LDAP://" + domain),
  45. "(&(objectCategory=user)(samAccountName=" + userName + "))",
  46. new[] { "memberOf" })).FindOne();
  47. return result.Properties["memberOf"].Contains(adminDn);
  48. }
  49.  
  50. public static string GetAdminDn(string domain) {
  51. return (string)(new DirectorySearcher(
  52. new DirectoryEntry("LDAP://" + domain),
  53. "(&(objectCategory=group)(cn=Domain Admins))")
  54. .FindOne().Properties["distinguishedname"][0]);
  55. }
  56.  
  57. static string BuildOctetString(SecurityIdentifier sid)
  58. {
  59. byte[] items = new byte[sid.BinaryLength];
  60. sid.GetBinaryForm(items, 0);
  61. StringBuilder sb = new StringBuilder();
  62. foreach (byte b in items)
  63. {
  64. sb.Append(b.ToString("X2"));
  65. }
  66. return sb.ToString();
  67. }
  68. public static bool IsDomainAdmin(string domain, string userName)
  69. {
  70. using (DirectoryEntry domainEntry = new DirectoryEntry(string.Format("LDAP://{0}", domain)))
  71. {
  72. byte[] domainSIdArray = (byte[])domainEntry.Properties["objectSid"].Value;
  73.  
  74. SecurityIdentifier domainSId = new SecurityIdentifier(domainSIdArray, 0);
  75. SecurityIdentifier domainAdminsSId = new SecurityIdentifier(WellKnownSidType.AccountDomainAdminsSid, domainSId);
  76.  
  77. using (DirectoryEntry groupEntry = new DirectoryEntry(string.Format("LDAP://<SID={0}>", BuildOctetString(domainAdminsSId))))
  78. {
  79. string adminDn = groupEntry.Properties["distinguishedname"].Value as string;
  80. SearchResult result = (new DirectorySearcher(domainEntry, string.Format("(&(objectCategory=user)(samAccountName={0}))", userName), new[] { "memberOf" })).FindOne();
  81. return result.Properties["memberOf"].Contains(adminDn);
  82. }
  83. }
  84. }
Add Comment
Please, Sign In to add comment