Advertisement
Guest User

Untitled

a guest
Jun 17th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 53.34 KB | None | 0 0
  1. <Window x:Class="ActiveDirectoryToolWpf.ActiveDirectoryToolView"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:ActiveDirectoryToolWpf"
  7. mc:Ignorable="d"
  8. Title="ActiveDirectoryToolView" Height="576" Width="1024" MinHeight="576" MinWidth="1024">
  9. <Grid Margin="0,0,0,0">
  10. <Grid.RowDefinitions>
  11. <RowDefinition Height="*" />
  12. </Grid.RowDefinitions>
  13. <Grid.ColumnDefinitions>
  14. <ColumnDefinition Width="25*" />
  15. <ColumnDefinition Width="75*" />
  16. </Grid.ColumnDefinitions>
  17. <Grid Grid.Row="0">
  18. <Grid Grid.Column="0">
  19. <TreeView x:Name="TreeView"
  20. ItemsSource="{Binding Path=Children}">
  21. <TreeView.ItemTemplate>
  22. <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
  23. <TextBlock Text="{Binding}" />
  24. </HierarchicalDataTemplate>
  25. </TreeView.ItemTemplate>
  26. </TreeView>
  27. </Grid>
  28. </Grid>
  29. <Grid Grid.Column="1">
  30. <Grid.RowDefinitions>
  31. <RowDefinition Height="42px" />
  32. <RowDefinition Height="*" />
  33. </Grid.RowDefinitions>
  34. <Grid Grid.Row="0">
  35. <Button x:Name="GetUsersButton" Content="Get Users" HorizontalAlignment="Left" Margin="10,10,0,0"
  36. VerticalAlignment="Top" Width="75" Click="GetUsersButton_Click" />
  37. <Button x:Name="GetUsersGroupsButton" Content="Get Users' Groups" HorizontalAlignment="Left"
  38. Margin="90,10,0,0" VerticalAlignment="Top" Width="103" Click="GetUsersGroupsButton_Click" />
  39. <Button x:Name="GetGroupsButton" Content="Get Groups" HorizontalAlignment="Left" Margin="198,10,0,0"
  40. VerticalAlignment="Top" Width="75" Click="GetGroupsButton_Click" />
  41. <Button x:Name="GetDirectReports" Content="Get Direct Reports" HorizontalAlignment="Left"
  42. Margin="278,10,0,0" VerticalAlignment="Top" Width="103" Click="GetDirectReports_Click" />
  43. </Grid>
  44. <Grid Grid.Row="1">
  45. <DataGrid x:Name="DataGrid"
  46. AutoGenerateColumns="True"
  47. HorizontalAlignment="Left"
  48. Margin="10,10,0,0"
  49. VerticalAlignment="Top"
  50. RenderTransformOrigin="2.25,-3.615"
  51. MaxHeight="9001" MaxWidth="90000000"
  52. ItemsSource="{Binding IsAsync=True}" MinHeight="484" MinWidth="742" />
  53. </Grid>
  54. </Grid>
  55. </Grid>
  56. </Window>
  57.  
  58. using System;
  59. using System.Data;
  60. using System.Windows;
  61.  
  62. namespace ActiveDirectoryToolWpf
  63. {
  64. public partial class ActiveDirectoryToolView : IActiveDirectoryToolView
  65. {
  66. public ActiveDirectoryToolView()
  67. {
  68. ViewModel = new ActiveDirectoryToolViewModel(this);
  69. DataContext = new ActiveDirectoryScopeFetcher().Scope;
  70. InitializeComponent();
  71. DataGrid.EnableColumnVirtualization = true;
  72. DataGrid.EnableRowVirtualization = true;
  73. }
  74.  
  75. public ActiveDirectoryToolViewModel ViewModel { get; }
  76.  
  77. public event Action GetComputersClicked;
  78.  
  79. public event Action GetDirectReportsClicked;
  80.  
  81. public event Action GetGroupsClicked;
  82.  
  83. public event Action GetUsersClicked;
  84.  
  85. public event Action GetUsersGroupsClicked;
  86.  
  87. public ActiveDirectoryScope Scope =>
  88. TreeView.SelectedItem as ActiveDirectoryScope;
  89.  
  90. public void SetDataGridData(DataView dataView)
  91. {
  92. DataGrid.ItemsSource = dataView;
  93. }
  94.  
  95. public void ShowMessage(string message)
  96. {
  97. MessageBox.Show(message);
  98. }
  99.  
  100. public void ToggleEnabled()
  101. {
  102. IsEnabled = !IsEnabled;
  103. }
  104.  
  105. private void GetDirectReports_Click(object sender, RoutedEventArgs e)
  106. {
  107. if (Scope != null)
  108. GetDirectReportsClicked?.Invoke();
  109. }
  110.  
  111. private void GetGroupsButton_Click(object sender, RoutedEventArgs e)
  112. {
  113. if (Scope != null)
  114. GetGroupsClicked?.Invoke();
  115. }
  116.  
  117. private void GetUsersButton_Click(object sender, RoutedEventArgs e)
  118. {
  119. if (Scope != null)
  120. GetUsersClicked?.Invoke();
  121. }
  122.  
  123. private void GetUsersGroupsButton_Click(object sender, RoutedEventArgs e)
  124. {
  125. if (Scope != null)
  126. GetUsersGroupsClicked?.Invoke();
  127. }
  128. }
  129. }
  130.  
  131. using System.Data;
  132. using System.Linq;
  133.  
  134. namespace ActiveDirectoryToolWpf
  135. {
  136. public class ActiveDirectoryToolViewModel
  137. {
  138. private readonly ActiveDirectoryAttribute[]
  139. _defaultDirectReportsAttributes =
  140. {
  141. ActiveDirectoryAttribute.UserDisplayName,
  142. ActiveDirectoryAttribute.UserSamAccountName,
  143. ActiveDirectoryAttribute.DirectReportDisplayName,
  144. ActiveDirectoryAttribute.DirectReportSamAccountName
  145. };
  146.  
  147. private readonly ActiveDirectoryAttribute[] _defaultGroupAttributes =
  148. {
  149. ActiveDirectoryAttribute.GroupSamAccountName,
  150. ActiveDirectoryAttribute.GroupManagedBy,
  151. ActiveDirectoryAttribute.GroupDescription,
  152. ActiveDirectoryAttribute.GroupDistinguishedName
  153. };
  154.  
  155. private readonly ActiveDirectoryAttribute[] _defaultUserAttributes =
  156. {
  157. ActiveDirectoryAttribute.UserSurname,
  158. ActiveDirectoryAttribute.UserGivenName,
  159. ActiveDirectoryAttribute.UserDisplayName,
  160. ActiveDirectoryAttribute.UserSamAccountName,
  161. ActiveDirectoryAttribute.UserIsActive,
  162. ActiveDirectoryAttribute.UserIsAccountLockedOut,
  163. ActiveDirectoryAttribute.UserDescription,
  164. ActiveDirectoryAttribute.UserTitle,
  165. ActiveDirectoryAttribute.UserCompany,
  166. ActiveDirectoryAttribute.UserManager,
  167. ActiveDirectoryAttribute.UserHomeDrive,
  168. ActiveDirectoryAttribute.UserHomeDirectory,
  169. ActiveDirectoryAttribute.UserScriptPath,
  170. ActiveDirectoryAttribute.UserEmailAddress,
  171. ActiveDirectoryAttribute.UserStreetAddress,
  172. ActiveDirectoryAttribute.UserCity,
  173. ActiveDirectoryAttribute.UserState,
  174. ActiveDirectoryAttribute.UserVoiceTelephoneNumber,
  175. ActiveDirectoryAttribute.UserPager,
  176. ActiveDirectoryAttribute.UserMobile,
  177. ActiveDirectoryAttribute.UserFax,
  178. ActiveDirectoryAttribute.UserVoip,
  179. ActiveDirectoryAttribute.UserSip,
  180. ActiveDirectoryAttribute.UserUserPrincipalName,
  181. ActiveDirectoryAttribute.UserDistinguishedName
  182. };
  183.  
  184. private readonly ActiveDirectoryAttribute[]
  185. _defaultUserGroupsAttributes =
  186. {
  187. ActiveDirectoryAttribute.UserSamAccountName,
  188. ActiveDirectoryAttribute.GroupSamAccountName,
  189. ActiveDirectoryAttribute.UserName,
  190. ActiveDirectoryAttribute.UserDistinguishedName
  191. };
  192.  
  193. private readonly IActiveDirectoryToolView _view;
  194. private DataPreparer _dataPreparer;
  195. private ActiveDirectorySearcher _searcher;
  196.  
  197. public ActiveDirectoryToolViewModel(IActiveDirectoryToolView view)
  198. {
  199. _view = view;
  200. _view.GetUsersClicked += OnGetUsers;
  201. _view.GetUsersGroupsClicked += OnGetUsersGroups;
  202. _view.GetDirectReportsClicked += OnGetDirectReports;
  203. _view.GetGroupsClicked += OnGetGroups;
  204. }
  205.  
  206. private void OnGetDirectReports()
  207. {
  208. _searcher = new ActiveDirectorySearcher(_view.Scope);
  209. _dataPreparer = new DataPreparer
  210. {
  211. Data = _searcher.GetDirectReports(),
  212. Attributes = _defaultDirectReportsAttributes.ToList()
  213. };
  214. _view.SetDataGridData(
  215. _dataPreparer.GetResults().ToDataTable().AsDataView());
  216. }
  217.  
  218. private void OnGetGroups()
  219. {
  220. _searcher = new ActiveDirectorySearcher(_view.Scope);
  221. _dataPreparer = new DataPreparer
  222. {
  223. Data = _searcher.GetGroups(),
  224. Attributes = _defaultGroupAttributes.ToList()
  225. };
  226. _view.SetDataGridData(
  227. _dataPreparer.GetResults().ToDataTable().AsDataView());
  228. }
  229.  
  230. private void OnGetUsers()
  231. {
  232. _searcher = new ActiveDirectorySearcher(_view.Scope);
  233. _dataPreparer = new DataPreparer
  234. {
  235. Data = _searcher.GetUsers(),
  236. Attributes = _defaultUserAttributes.ToList()
  237. };
  238. _view.SetDataGridData(
  239. _dataPreparer.GetResults().ToDataTable().AsDataView());
  240. }
  241.  
  242. private void OnGetUsersGroups()
  243. {
  244. _searcher = new ActiveDirectorySearcher(_view.Scope);
  245. _dataPreparer = new DataPreparer
  246. {
  247. Data = _searcher.GetUsersGroups(),
  248. Attributes = _defaultUserGroupsAttributes.ToList()
  249. };
  250. _view.SetDataGridData(
  251. _dataPreparer.GetResults().ToDataTable().AsDataView());
  252. }
  253. }
  254. }
  255.  
  256. using System;
  257. using System.Collections.Generic;
  258. using PrimitiveExtensions;
  259.  
  260. namespace ActiveDirectoryToolWpf
  261. {
  262. public class ActiveDirectoryScope : IEquatable<ActiveDirectoryScope>
  263. {
  264. private const char Comma = ',';
  265. private const string DomainComponentPrefix = "DC=";
  266. private const string LdapProtocolPrefix = "LDAP://";
  267. private const char Period = '.';
  268.  
  269. internal ActiveDirectoryScope()
  270. {
  271. Children = new List<ActiveDirectoryScope>();
  272. }
  273.  
  274. public List<ActiveDirectoryScope> Children { get; set; }
  275. internal string Context => Path.Remove(LdapProtocolPrefix);
  276.  
  277. internal string Domain
  278. => Path.SubstringAtIndexOfOrdinal(DomainComponentPrefix)
  279. .Remove(DomainComponentPrefix)
  280. .Replace(Comma, Period);
  281.  
  282. internal string Name { get; set; }
  283. internal string Path { get; set; }
  284.  
  285. public bool Equals(ActiveDirectoryScope other)
  286. {
  287. return Name == other.Name;
  288. }
  289.  
  290. public override string ToString()
  291. {
  292. return Name;
  293. }
  294.  
  295. internal void AddDirectoryScope(OrganizationalUnit organizationalUnit)
  296. {
  297. if (organizationalUnit.Split == null ||
  298. organizationalUnit.Split.Length < 1)
  299. {
  300. throw new ArgumentException(
  301. "The organizational units array is null or empty!");
  302. }
  303. var parent = this;
  304. foreach (var level in organizationalUnit.Split)
  305. {
  306. var lastLevel = organizationalUnit.Split.Length - 1;
  307. if (parent.Children.Contains(new ActiveDirectoryScope
  308. {
  309. Name = level
  310. }))
  311. {
  312. parent = parent.Children.Find(
  313. item => item.Name.Equals(level));
  314. }
  315. else if (level == organizationalUnit.Split[lastLevel])
  316. {
  317. parent.Children.Add(new ActiveDirectoryScope
  318. {
  319. Name = level,
  320. Path = organizationalUnit.Path
  321. });
  322. }
  323. }
  324. }
  325. }
  326. }
  327.  
  328. using System.DirectoryServices;
  329. using System.DirectoryServices.AccountManagement;
  330.  
  331. namespace ActiveDirectoryToolWpf
  332. {
  333. public class ActiveDirectoryScopeFetcher
  334. {
  335. internal ActiveDirectoryScopeFetcher()
  336. {
  337. var rootPrincipalContext = new PrincipalContext(
  338. ContextType.Domain);
  339. var rootDirectoryEntry = new DirectoryEntry(
  340. rootPrincipalContext.ConnectedServer);
  341. Scope = new ActiveDirectoryScope
  342. {
  343. Name = rootDirectoryEntry.Path,
  344. Path = "LDAP://" + rootDirectoryEntry.Path
  345. };
  346. FetchScopeList();
  347. }
  348.  
  349. public ActiveDirectoryScope Scope { get; set; }
  350.  
  351. private void FetchScopeList()
  352. {
  353. using (var directorySearcher = new DirectorySearcher(Scope.Path))
  354. {
  355. directorySearcher.Filter =
  356. "(objectCategory=organizationalUnit)";
  357. directorySearcher.PageSize = 1;
  358. foreach (SearchResult result in directorySearcher.FindAll())
  359. {
  360. Scope.AddDirectoryScope(new OrganizationalUnit
  361. {
  362. Path = result.Path
  363. });
  364. }
  365. }
  366. Scope.Children.Sort((a, b) => a.Name.CompareTo(b.Name));
  367. }
  368. }
  369. }
  370.  
  371. using System;
  372. using System.Collections.Generic;
  373. using System.Diagnostics;
  374. using System.DirectoryServices.AccountManagement;
  375. using System.Linq;
  376.  
  377. namespace ActiveDirectoryToolWpf
  378. {
  379. public class ActiveDirectorySearcher
  380. {
  381. public ActiveDirectorySearcher(ActiveDirectoryScope scope)
  382. {
  383. Scope = scope;
  384. }
  385.  
  386. private PrincipalContext PrincipalContext => new PrincipalContext(
  387. ContextType.Domain, Scope.Domain, Scope.Context);
  388.  
  389. private ActiveDirectoryScope Scope { get; }
  390.  
  391. public static IEnumerable<ComputerPrincipal> GetComputersFromContext(
  392. PrincipalContext context)
  393. {
  394. IEnumerable<ComputerPrincipal> computers;
  395. using (var searcher = new PrincipalSearcher(
  396. new ComputerPrincipal(context)))
  397. {
  398. computers =
  399. searcher.FindAll().OfType<ComputerPrincipal>().ToList();
  400. }
  401. return computers;
  402. }
  403.  
  404. public static IEnumerable<ComputerPrincipal> GetComputersFromGroup(
  405. GroupPrincipal group)
  406. {
  407. return group.GetMembers().OfType<ComputerPrincipal>();
  408. }
  409.  
  410. public static IEnumerable<DirectReports> GetDirectReportsFromContext(
  411. PrincipalContext context)
  412. {
  413. return GetDirectReportsFromUsers(GetUsersFromContext(context));
  414. }
  415.  
  416. public static IEnumerable<DirectReports> GetDirectReportsFromUsers(
  417. IEnumerable<UserPrincipal> users)
  418. {
  419. return users.Select(user => new DirectReports
  420. {
  421. User = user,
  422. Reports = user.GetDirectReports()
  423. }).ToList();
  424. }
  425.  
  426. public static IEnumerable<GroupPrincipal> GetGroupsFromContext(
  427. PrincipalContext context)
  428. {
  429. return GetGroupsFromUsers(GetUsersFromContext(context));
  430. }
  431.  
  432. public static IEnumerable<GroupPrincipal> GetGroupsFromUsers(
  433. IEnumerable<UserPrincipal> users)
  434. {
  435. var groups = new HashSet<GroupPrincipal>();
  436. foreach (var user in users)
  437. {
  438. groups.UnionWith(
  439. user.GetGroups().OfType<GroupPrincipal>().ToList());
  440. }
  441.  
  442. return groups;
  443. }
  444.  
  445. public static IEnumerable<UserGroups> GetUserGroupsFromContext(
  446. PrincipalContext context)
  447. {
  448. return GetUserGroupsFromUsers(GetUsersFromContext(context));
  449. }
  450.  
  451. public static IEnumerable<UserGroups> GetUserGroupsFromUsers(
  452. IEnumerable<UserPrincipal> users)
  453. {
  454. var userGroups = new List<UserGroups>();
  455. foreach (var user in users)
  456. {
  457. var groups = new List<GroupPrincipal>();
  458. try
  459. {
  460. groups.AddRange(user.GetGroups().OfType<GroupPrincipal>());
  461. }
  462. catch (Exception e)
  463. {
  464. Debug.WriteLine(e.Message);
  465. }
  466. userGroups.Add(new UserGroups
  467. {
  468. User = user,
  469. Groups = groups
  470. });
  471. }
  472. return userGroups;
  473. }
  474.  
  475. public static IEnumerable<UserPrincipal> GetUsersFromContext(
  476. PrincipalContext context)
  477. {
  478. IEnumerable<UserPrincipal> users;
  479. using (var searcher = new PrincipalSearcher(new UserPrincipal(
  480. context)))
  481. {
  482. users = searcher.FindAll().OfType<UserPrincipal>().ToList();
  483. }
  484. return users;
  485. }
  486.  
  487. public static IEnumerable<UserPrincipal> GetUsersFromGroup(
  488. GroupPrincipal group)
  489. {
  490. return group.GetMembers().OfType<UserPrincipal>().ToList();
  491. }
  492.  
  493. public IEnumerable<DirectReports> GetDirectReports()
  494. {
  495. return GetDirectReportsFromContext(PrincipalContext);
  496. }
  497.  
  498. public IEnumerable<GroupPrincipal> GetGroups()
  499. {
  500. return GetGroupsFromContext(PrincipalContext);
  501. }
  502.  
  503. public IEnumerable<UserPrincipal> GetUsers()
  504. {
  505. return GetUsersFromContext(PrincipalContext);
  506. }
  507.  
  508. public IEnumerable<UserGroups> GetUsersGroups()
  509. {
  510. return GetUserGroupsFromContext(PrincipalContext);
  511. }
  512. }
  513.  
  514. public class DirectReports : ExtendedPrincipalBase, IDisposable
  515. {
  516. public IEnumerable<UserPrincipal> Reports { get; set; }
  517.  
  518. public void Dispose()
  519. {
  520. User?.Dispose();
  521. foreach (var report in Reports)
  522. report?.Dispose();
  523. }
  524. }
  525.  
  526. public abstract class ExtendedPrincipalBase
  527. {
  528. public UserPrincipal User { get; set; }
  529. }
  530.  
  531. public class UserGroups : ExtendedPrincipalBase, IDisposable
  532. {
  533. public IEnumerable<GroupPrincipal> Groups { get; set; }
  534.  
  535. public void Dispose()
  536. {
  537. User?.Dispose();
  538. foreach (var group in Groups)
  539. group?.Dispose();
  540. }
  541. }
  542. }
  543.  
  544. using System;
  545. using System.Collections.Generic;
  546. using System.Data;
  547. using System.DirectoryServices.AccountManagement;
  548. using System.Dynamic;
  549. using System.Linq;
  550. using static ActiveDirectoryToolWpf.ActiveDirectoryAttribute;
  551.  
  552. namespace ActiveDirectoryToolWpf
  553. {
  554. public enum ActiveDirectoryAttribute
  555. {
  556. ComputerAccountExpirationDate,
  557. ComputerAccountLockoutTime,
  558. ComputerAllowReversiblePasswordEncryption,
  559. ComputerBadLogonCount,
  560. ComputerCertificates,
  561. ComputerContext,
  562. ComputerContextType,
  563. ComputerDelegationPermitted,
  564. ComputerDescription,
  565. ComputerDisplayName,
  566. ComputerDistinguishedName,
  567. ComputerEnabled,
  568. ComputerGuid,
  569. ComputerHomeDirectory,
  570. ComputerHomeDrive,
  571. ComputerLastBadPasswordAttempt,
  572. ComputerLastLogon,
  573. ComputerLastPasswordSet,
  574. ComputerName,
  575. ComputerPasswordNeverExpires,
  576. ComputerPasswordNotRequired,
  577. ComputerPermittedLogonTimes,
  578. ComputerPermittedWorkstations,
  579. ComputerSamAccountName,
  580. ComputerScriptPath,
  581. ComputerServicePrincipalNames,
  582. ComputerSid,
  583. ComputerSmartcardLogonRequired,
  584. ComputerStructuralObjectClass,
  585. ComputerUserCannotChangePassword,
  586. ComputerUserPrincipalName,
  587. DirectReportUserAccountControl,
  588. DirectReportAccountExpirationDate,
  589. DirectReportAccountLockoutTime,
  590. DirectReportAllowReversiblePasswordEncryption,
  591. DirectReportAssistant,
  592. DirectReportBadLogonCount,
  593. DirectReportCertificates,
  594. DirectReportCity,
  595. DirectReportComment,
  596. DirectReportCompany,
  597. DirectReportContext,
  598. DirectReportContextType,
  599. DirectReportCountry,
  600. DirectReportDelegationPermitted,
  601. DirectReportDepartment,
  602. DirectReportDescription,
  603. DirectReportDisplayName,
  604. DirectReportDistinguishedName,
  605. DirectReportDivision,
  606. DirectReportEmailAddress,
  607. DirectReportEmployeeId,
  608. DirectReportEnabled,
  609. DirectReportFax,
  610. DirectReportSuffix,
  611. DirectReportGivenName,
  612. DirectReportGuid,
  613. DirectReportHomeAddress,
  614. DirectReportHomeDirectory,
  615. DirectReportHomeDrive,
  616. DirectReportHomePhone,
  617. DirectReportInitials,
  618. DirectReportIsAccountLockedOut,
  619. DirectReportIsActive,
  620. DirectReportLastBadPasswordAttempt,
  621. DirectReportLastLogon,
  622. DirectReportLastPasswordSet,
  623. DirectReportManager,
  624. DirectReportMiddleName,
  625. DirectReportMobile,
  626. DirectReportName,
  627. DirectReportNotes,
  628. DirectReportPager,
  629. DirectReportPasswordNeverExpires,
  630. DirectReportPasswordNotRequired,
  631. DirectReportPermittedLogonTimes,
  632. DirectReportPermittedWorkstations,
  633. DirectReportSamAccountName,
  634. DirectReportScriptPath,
  635. DirectReportSid,
  636. DirectReportSip,
  637. DirectReportSmartcardLogonRequired,
  638. DirectReportState,
  639. DirectReportStreetAddress,
  640. DirectReportStructuralObjectClass,
  641. DirectReportSurname,
  642. DirectReportTitle,
  643. DirectReportUserCannotChangePassword,
  644. DirectReportUserPrincipalName,
  645. DirectReportVoiceTelephoneNumber,
  646. DirectReportVoip,
  647. GroupContext,
  648. GroupContextType,
  649. GroupDescription,
  650. GroupDisplayName,
  651. GroupDistinguishedName,
  652. GroupGuid,
  653. GroupIsSecurityGroup,
  654. GroupManagedBy,
  655. GroupMembers,
  656. GroupName,
  657. GroupSamAccountName,
  658. GroupScope,
  659. GroupSid,
  660. GroupStructuralObjectClass,
  661. GroupUserPrincipalName,
  662. UserUserAccountControl,
  663. UserAccountExpirationDate,
  664. UserAccountLockoutTime,
  665. UserAllowReversiblePasswordEncryption,
  666. UserAssistant,
  667. UserBadLogonCount,
  668. UserCertificates,
  669. UserCity,
  670. UserComment,
  671. UserCompany,
  672. UserContext,
  673. UserContextType,
  674. UserCountry,
  675. UserDelegationPermitted,
  676. UserDepartment,
  677. UserDescription,
  678. UserDisplayName,
  679. UserDistinguishedName,
  680. UserDivision,
  681. UserEmailAddress,
  682. UserEmployeeId,
  683. UserEnabled,
  684. UserFax,
  685. UserSuffix,
  686. UserGivenName,
  687. UserGuid,
  688. UserHomeAddress,
  689. UserHomeDirectory,
  690. UserHomeDrive,
  691. UserHomePhone,
  692. UserInitials,
  693. UserIsAccountLockedOut,
  694. UserIsActive,
  695. UserLastBadPasswordAttempt,
  696. UserLastLogon,
  697. UserLastPasswordSet,
  698. UserManager,
  699. UserMiddleName,
  700. UserMobile,
  701. UserName,
  702. UserNotes,
  703. UserPager,
  704. UserPasswordNeverExpires,
  705. UserPasswordNotRequired,
  706. UserPermittedLogonTimes,
  707. UserPermittedWorkstations,
  708. UserSamAccountName,
  709. UserScriptPath,
  710. UserSid,
  711. UserSip,
  712. UserSmartcardLogonRequired,
  713. UserState,
  714. UserStreetAddress,
  715. UserStructuralObjectClass,
  716. UserSurname,
  717. UserTitle,
  718. UserUserCannotChangePassword,
  719. UserUserPrincipalName,
  720. UserVoiceTelephoneNumber,
  721. UserVoip
  722. }
  723.  
  724. internal static class Extensions
  725. {
  726. public static DataTable ToDataTable(this IEnumerable<dynamic> items)
  727. {
  728. var data = items.ToArray();
  729. if (!data.Any())
  730. {
  731. return null;
  732. }
  733.  
  734. var dataTable = new DataTable();
  735. foreach (var key in ((IDictionary<string, object>) data[0]).Keys)
  736. {
  737. dataTable.Columns.Add(key);
  738. }
  739. foreach (var d in data)
  740. {
  741. dataTable.Rows.Add(
  742. ((IDictionary<string, object>) d).Values.ToArray());
  743. }
  744. return dataTable;
  745. }
  746. }
  747.  
  748. internal class DataPreparer
  749. {
  750. internal List<ActiveDirectoryAttribute> Attributes { get; set; }
  751. internal IEnumerable<object> Data { get; set; }
  752.  
  753. internal List<ExpandoObject> GetResults()
  754. {
  755. var results = new List<ExpandoObject>();
  756.  
  757. foreach (var data in Data)
  758. {
  759. GroupPrincipal groupPrincipal;
  760. UserPrincipal userPrincipal;
  761. if (data is UserGroups)
  762. {
  763. var userGroups = data as UserGroups;
  764. userPrincipal = userGroups.User;
  765. foreach (var group in userGroups.Groups)
  766. {
  767. groupPrincipal = group;
  768. dynamic result = new ExpandoObject();
  769. AddAttributesToResult(
  770. null, groupPrincipal, null, userPrincipal, result);
  771. results.Add(result);
  772. }
  773. userGroups.Dispose();
  774. }
  775. else if (data is DirectReports)
  776. {
  777. var directReports = data as DirectReports;
  778. if (directReports.Reports == null) continue;
  779. userPrincipal = directReports.User;
  780. foreach (var directReport in directReports.Reports)
  781. {
  782. dynamic result = new ExpandoObject();
  783. AddAttributesToResult(
  784. null, null, directReport, userPrincipal, result);
  785. results.Add(result);
  786. }
  787. directReports.Dispose();
  788. }
  789. else
  790. {
  791. var computerPrincipal = data as ComputerPrincipal;
  792. groupPrincipal = data as GroupPrincipal;
  793. userPrincipal = data as UserPrincipal;
  794. dynamic result = new ExpandoObject();
  795. AddAttributesToResult(
  796. computerPrincipal,
  797. groupPrincipal,
  798. null,
  799. userPrincipal,
  800. result);
  801. results.Add(result);
  802. var principal = data as Principal;
  803. principal?.Dispose();
  804. }
  805. }
  806. return results;
  807. }
  808.  
  809. private static bool AddComputerAttributeToResult(
  810. ComputerPrincipal principal,
  811. ActiveDirectoryAttribute attribute,
  812. dynamic result)
  813. {
  814. var attributeMapping =
  815. new Dictionary<ActiveDirectoryAttribute, Action>
  816. {
  817. [ComputerAccountExpirationDate] = () =>
  818. result.ComputerAccountExpirationDate =
  819. principal.AccountExpirationDate,
  820. [ComputerAccountLockoutTime] = () =>
  821. result.ComputerAccountLockoutTime =
  822. principal.AccountLockoutTime,
  823. [ComputerAllowReversiblePasswordEncryption] = () =>
  824. result.ComputerAllowReversiblePasswordEncryption =
  825. principal.AllowReversiblePasswordEncryption,
  826. [ComputerBadLogonCount] = () =>
  827. result.ComputerBadLogonCount = principal.BadLogonCount,
  828. [ComputerCertificates] = () =>
  829. result.ComputerCertificates = principal.Certificates,
  830. [ComputerContext] = () =>
  831. result.ComputerContext = principal.Context,
  832. [ComputerContextType] = () =>
  833. result.ComputerContextType = principal.ContextType,
  834. [ComputerDelegationPermitted] = () =>
  835. result.ComputerDelegationPermitted =
  836. principal.DelegationPermitted,
  837. [ComputerDescription] = () =>
  838. result.ComputerDescription = principal.Description,
  839. [ComputerDisplayName] = () =>
  840. result.ComputerDisplayName = principal.DisplayName,
  841. [ComputerDistinguishedName] = () =>
  842. result.ComputerDistinguishedName =
  843. principal.DistinguishedName,
  844. [ComputerEnabled] = () =>
  845. result.ComputerEnabled = principal.Enabled,
  846. [ComputerGuid] = () => result.ComputerGuid =
  847. principal.Guid,
  848. [ComputerHomeDirectory] = () =>
  849. result.ComputerHomeDirectory = principal.HomeDirectory,
  850. [ComputerHomeDrive] = () =>
  851. result.ComputerHomeDrive = principal.HomeDrive,
  852. [ComputerLastBadPasswordAttempt] = () =>
  853. result.ComputerLastBadPasswordAttempt =
  854. principal.LastBadPasswordAttempt,
  855. [ComputerLastLogon] = () =>
  856. result.ComputerLastLogon = principal.LastLogon,
  857. [ComputerLastPasswordSet] = () =>
  858. result.LastPasswordSet = principal.LastPasswordSet,
  859. [ComputerName] = () => result.ComputerName =
  860. principal.Name,
  861. [ComputerPasswordNeverExpires] = () =>
  862. result.ComputerPasswordNeverExpires =
  863. principal.PasswordNeverExpires,
  864. [ComputerPasswordNotRequired] = () =>
  865. result.ComputerPasswordNotRequired =
  866. principal.PasswordNotRequired,
  867. [ComputerPermittedLogonTimes] = () =>
  868. result.ComputerPermittedLogonTimes =
  869. principal.PermittedLogonTimes,
  870. [ComputerPermittedWorkstations] = () =>
  871. result.ComputerPermittedWorkstations =
  872. principal.PermittedWorkstations,
  873. [ComputerSamAccountName] = () =>
  874. result.ComputerSamAccountName =
  875. principal.SamAccountName,
  876. [ComputerScriptPath] = () =>
  877. result.ComputerScriptPath = principal.ScriptPath,
  878. [ComputerServicePrincipalNames] = () =>
  879. result.ComputerServicePrincipalNames =
  880. principal.ServicePrincipalNames,
  881. [ComputerSid] = () => result.ComputerSid = principal.Sid,
  882. [ComputerSmartcardLogonRequired] = () =>
  883. result.ComputerSmartcardLogonRequired =
  884. principal.SmartcardLogonRequired,
  885. [ComputerStructuralObjectClass] = () =>
  886. result.ComputerStructuralObjectClass =
  887. principal.StructuralObjectClass,
  888. [ComputerUserCannotChangePassword] = () =>
  889. result.ComputerUserCannotChangePassword =
  890. principal.UserCannotChangePassword,
  891. [ComputerUserPrincipalName] = () =>
  892. result.ComputerUserPrincipalName =
  893. principal.UserPrincipalName
  894. };
  895.  
  896. if (!attributeMapping.ContainsKey(attribute)) return false;
  897. attributeMapping[attribute]();
  898. return true;
  899. }
  900.  
  901. private static bool AddDirectReportAttributeToResult(
  902. UserPrincipal principal,
  903. ActiveDirectoryAttribute attribute,
  904. dynamic result)
  905. {
  906. var attributeMapping =
  907. new Dictionary<ActiveDirectoryAttribute, Action>
  908. {
  909. [DirectReportUserAccountControl] = () =>
  910. result.DirectReportAccountControl =
  911. principal.GetUserAccountControl(),
  912. [DirectReportAccountExpirationDate] = () =>
  913. result.DirectReportAccountExpirationDate =
  914. principal.AccountExpirationDate,
  915. [DirectReportAccountLockoutTime] = () =>
  916. result.DirectReportAccountLockoutTime =
  917. principal.AccountLockoutTime,
  918. [DirectReportAllowReversiblePasswordEncryption] = () =>
  919. result.DirectReportAllowReversiblePasswordEncryption =
  920. principal.AllowReversiblePasswordEncryption,
  921. [DirectReportAssistant] = () =>
  922. result.DirectReportAssistant =
  923. principal.GetAssistant(),
  924. [DirectReportBadLogonCount] = () =>
  925. result.DirectReportBadLogonCount =
  926. principal.BadLogonCount,
  927. [DirectReportCertificates] = () =>
  928. result.DirectReportCertificates =
  929. principal.Certificates,
  930. [DirectReportCity] = () =>
  931. result.DirectReportCity = principal.GetCity(),
  932. [DirectReportComment] = () =>
  933. result.DirectReportComment = principal.GetComment(),
  934. [DirectReportCompany] = () =>
  935. result.DirectReportCompany = principal.GetCompany(),
  936. [DirectReportContext] = () =>
  937. result.DirectReportContext = principal.Context,
  938. [DirectReportContextType] = () =>
  939. result.DirectReportContextType = principal.ContextType,
  940. [DirectReportCountry] = () =>
  941. result.DirectReportCountry = principal.GetCountry(),
  942. [DirectReportDelegationPermitted] = () =>
  943. result.DirectReportDelegationPermitted =
  944. principal.DelegationPermitted,
  945. [DirectReportDepartment] = () =>
  946. result.DirectReportDepartment =
  947. principal.GetDepartment(),
  948. [DirectReportDescription] = () =>
  949. result.DirectReportDescription = principal.Description,
  950. [DirectReportDisplayName] = () =>
  951. result.DirectReportDisplayName = principal.DisplayName,
  952. [DirectReportDistinguishedName] = () =>
  953. result.DirectReportDistinguishedName =
  954. principal.DistinguishedName,
  955. [DirectReportDivision] = () =>
  956. result.DirectReportDivision = principal.GetDivision(),
  957. [DirectReportEmailAddress] = () =>
  958. result.DirectReportEmailAddress =
  959. principal.EmailAddress,
  960. [DirectReportEmployeeId] = () =>
  961. result.DirectReportEmployeeId = principal.EmployeeId,
  962. [DirectReportEnabled] = () =>
  963. result.DirectReportEnabled = principal.Enabled,
  964. [DirectReportFax] = () =>
  965. result.DirectReportFax = principal.GetFax(),
  966. [DirectReportSuffix] = () =>
  967. result.DirectReportSuffix = principal.GetSuffix(),
  968. [DirectReportGivenName] = () =>
  969. result.DirectReportGivenName = principal.GivenName,
  970. [DirectReportGuid] = () =>
  971. result.DirectReportGuid = principal.Guid,
  972. [DirectReportHomeAddress] = () =>
  973. result.DirectReportHomeAddress =
  974. principal.GetHomeAddress(),
  975. [DirectReportHomeDirectory] = () =>
  976. result.DirectReportHomeDirectory =
  977. principal.HomeDirectory,
  978. [DirectReportHomeDrive] = () =>
  979. result.DirectReportHomeDrive = principal.HomeDrive,
  980. [DirectReportHomePhone] = () =>
  981. result.DirectReportHomePhone =
  982. principal.GetHomePhone(),
  983. [DirectReportInitials] = () =>
  984. result.DirectReportInitials = principal.GetInitials(),
  985. [DirectReportIsAccountLockedOut] = () =>
  986. result.DirectReportIsAccountLockedOut =
  987. principal.IsAccountLockedOut(),
  988. [DirectReportIsActive] = () =>
  989. result.DirectReportIsActive = principal.IsActive(),
  990. [DirectReportLastBadPasswordAttempt] = () =>
  991. result.DirectReportLastBadPasswordAttempt =
  992. principal.LastBadPasswordAttempt,
  993. [DirectReportLastLogon] = () =>
  994. result.DirectReportLastLogon = principal.LastLogon,
  995. [DirectReportLastPasswordSet] = () =>
  996. result.DirectReportLastPasswordSet =
  997. principal.LastPasswordSet,
  998. [DirectReportManager] = () =>
  999. result.DirectReportManager = principal.GetManager(),
  1000. [DirectReportMiddleName] = () =>
  1001. result.DirectReportMiddleName = principal.MiddleName,
  1002. [DirectReportMobile] = () =>
  1003. result.DirectReportMobile = principal.GetMobile(),
  1004. [DirectReportName] = () =>
  1005. result.DirectReportName = principal.Name,
  1006. [DirectReportNotes] = () =>
  1007. result.DirectReportNotes = principal.GetNotes(),
  1008. [DirectReportPager] = () =>
  1009. result.DirectReportPager = principal.GetPager(),
  1010. [DirectReportPasswordNeverExpires] = () =>
  1011. result.DirectReportPasswordNeverExpires =
  1012. principal.PasswordNeverExpires,
  1013. [DirectReportPasswordNotRequired] = () =>
  1014. result.DirectReportPasswordNotRequired =
  1015. principal.PasswordNotRequired,
  1016. [DirectReportPermittedLogonTimes] = () =>
  1017. result.DirectReportPermittedLogonTimes =
  1018. principal.PermittedLogonTimes,
  1019. [DirectReportPermittedWorkstations] = () =>
  1020. result.DirectReportPermittedWorkstations =
  1021. principal.PermittedWorkstations,
  1022. [DirectReportSamAccountName] = () =>
  1023. result.DirectReportSamAccountName =
  1024. principal.SamAccountName,
  1025. [DirectReportScriptPath] = () =>
  1026. result.DirectReportScriptPath = principal.ScriptPath,
  1027. [DirectReportSid] = () =>
  1028. result.DirectReportSid = principal.Sid,
  1029. [DirectReportSip] = () =>
  1030. result.DirectReportSip = principal.GetSip(),
  1031. [DirectReportSmartcardLogonRequired] = () =>
  1032. result.DirectReportSmartcardLogonRequired =
  1033. principal.SmartcardLogonRequired,
  1034. [DirectReportState] = () =>
  1035. result.DirectReportState = principal.GetState(),
  1036. [DirectReportStreetAddress] = () =>
  1037. result.DirectReportStreetAddress =
  1038. principal.GetStreetAddress(),
  1039. [DirectReportStructuralObjectClass] = () =>
  1040. result.DirectReportStructuralObjectClass =
  1041. principal.StructuralObjectClass,
  1042. [DirectReportSurname] = () =>
  1043. result.DirectReportSurname = principal.Surname,
  1044. [DirectReportTitle] = () =>
  1045. result.DirectReportTitle = principal.GetTitle(),
  1046. [DirectReportUserCannotChangePassword] = () =>
  1047. result.DirectReportUserCannotChangePassword =
  1048. principal.UserCannotChangePassword,
  1049. [DirectReportUserPrincipalName] = () =>
  1050. result.DirectReportUserPrincipalName =
  1051. principal.UserPrincipalName,
  1052. [DirectReportVoiceTelephoneNumber] = () =>
  1053. result.DirectReportVoiceTelephoneNumber =
  1054. principal.VoiceTelephoneNumber,
  1055. [DirectReportVoip] = () =>
  1056. result.DirectReportVoip = principal.GetVoip()
  1057. };
  1058.  
  1059. if (!attributeMapping.ContainsKey(attribute)) return false;
  1060. attributeMapping[attribute]();
  1061. return true;
  1062. }
  1063.  
  1064. private static bool AddGroupAttributeToResult(
  1065. GroupPrincipal principal,
  1066. ActiveDirectoryAttribute attribute,
  1067. dynamic result)
  1068. {
  1069. var attributeMapping =
  1070. new Dictionary<ActiveDirectoryAttribute, Action>
  1071. {
  1072. [GroupContext] =
  1073. () => result.GroupContext = principal.Context,
  1074. [GroupContextType] = () =>
  1075. result.GroupContextType = principal.ContextType,
  1076. [GroupDescription] = () =>
  1077. result.GroupDescription = principal.Description,
  1078. [GroupDisplayName] = () =>
  1079. result.GroupDisplayName = principal.DisplayName,
  1080. [GroupDistinguishedName] = () =>
  1081. result.GroupDistinguishedName =
  1082. principal.DistinguishedName,
  1083. [GroupGuid] = () => result.GroupGuid = principal.Guid,
  1084. [GroupIsSecurityGroup] = () =>
  1085. result.GroupIsSecurityGroup =
  1086. principal.IsSecurityGroup,
  1087. [GroupManagedBy] = () =>
  1088. result.GroupManagedBy = principal.GetManagedBy(),
  1089. [GroupName] = () => result.GroupName = principal.Name,
  1090. [GroupSamAccountName] = () =>
  1091. result.GroupSamAccountName = principal.SamAccountName,
  1092. [ActiveDirectoryAttribute.GroupScope] = () =>
  1093. result.GroupScope = principal.GroupScope,
  1094. [GroupSid] = () => result.GroupSid = principal.Sid,
  1095. [GroupStructuralObjectClass] = () =>
  1096. result.GroupStructuralObjectClass =
  1097. principal.StructuralObjectClass,
  1098. [GroupUserPrincipalName] = () =>
  1099. result.GroupUserPrincipalName =
  1100. principal.UserPrincipalName,
  1101. [GroupMembers] =
  1102. () => result.GroupMembers = principal.Members
  1103. };
  1104.  
  1105. if (!attributeMapping.ContainsKey(attribute)) return false;
  1106. attributeMapping[attribute]();
  1107. return true;
  1108. }
  1109.  
  1110. private static void AddUserAttributeToResult(
  1111. UserPrincipal principal,
  1112. ActiveDirectoryAttribute attribute,
  1113. dynamic result)
  1114. {
  1115. var attributeMapping =
  1116. new Dictionary<ActiveDirectoryAttribute, Action>
  1117. {
  1118. [UserUserAccountControl] = () =>
  1119. result.UserAccountControl =
  1120. principal.GetUserAccountControl(),
  1121. [UserAccountExpirationDate] = () =>
  1122. result.UserAccountExpirationDate =
  1123. principal.AccountExpirationDate,
  1124. [UserAccountLockoutTime] = () =>
  1125. result.UserAccountLockoutTime =
  1126. principal.AccountLockoutTime,
  1127. [UserAllowReversiblePasswordEncryption] = () =>
  1128. result.UserAllowReversiblePasswordEncryption =
  1129. principal.AllowReversiblePasswordEncryption,
  1130. [UserAssistant] = () =>
  1131. result.UserAssistant = principal.GetAssistant(),
  1132. [UserBadLogonCount] = () =>
  1133. result.UserBadLogonCount = principal.BadLogonCount,
  1134. [UserCertificates] = () =>
  1135. result.UserCertificates = principal.Certificates,
  1136. [UserCity] = () =>
  1137. result.UserCity = principal.GetCity(),
  1138. [UserComment] = () =>
  1139. result.UserComment = principal.GetComment(),
  1140. [UserCompany] = () =>
  1141. result.UserCompany = principal.GetCompany(),
  1142. [UserContext] = () =>
  1143. result.UserContext = principal.Context,
  1144. [UserContextType] = () =>
  1145. result.UserContextType = principal.ContextType,
  1146. [UserCountry] = () =>
  1147. result.UserCountry = principal.GetCountry(),
  1148. [UserDelegationPermitted] = () =>
  1149. result.UserDelegationPermitted =
  1150. principal.DelegationPermitted,
  1151. [UserDepartment] = () =>
  1152. result.UserDepartment = principal.GetDepartment(),
  1153. [UserDescription] = () =>
  1154. result.UserDescription = principal.Description,
  1155. [UserDisplayName] = () =>
  1156. result.UserDisplayName = principal.DisplayName,
  1157. [UserDistinguishedName] = () =>
  1158. result.UserDistinguishedName =
  1159. principal.DistinguishedName,
  1160. [UserDivision] = () =>
  1161. result.UserDivision = principal.GetDivision(),
  1162. [UserEmailAddress] = () =>
  1163. result.UserEmailAddress = principal.EmailAddress,
  1164. [UserEmployeeId] = () =>
  1165. result.UserEmployeeId = principal.EmployeeId,
  1166. [UserEnabled] = () =>
  1167. result.UserEnabled = principal.Enabled,
  1168. [UserFax] = () =>
  1169. result.UserFax = principal.GetFax(),
  1170. [UserSuffix] = () =>
  1171. result.UserSuffix = principal.GetSuffix(),
  1172. [UserGivenName] = () =>
  1173. result.UserGivenName = principal.GivenName,
  1174. [UserGuid] = () =>
  1175. result.UserGuid = principal.Guid,
  1176. [UserHomeAddress] = () =>
  1177. result.UserHomeAddress = principal.GetHomeAddress(),
  1178. [UserHomeDirectory] = () =>
  1179. result.UserHomeDirectory = principal.HomeDirectory,
  1180. [UserHomeDrive] = () =>
  1181. result.UserHomeDrive = principal.HomeDrive,
  1182. [UserHomePhone] = () =>
  1183. result.UserHomePhone = principal.GetHomePhone(),
  1184. [UserInitials] = () =>
  1185. result.UserInitials = principal.GetInitials(),
  1186. [UserIsAccountLockedOut] = () =>
  1187. result.UserIsAccountLockedOut =
  1188. principal.IsAccountLockedOut(),
  1189. [UserIsActive] = () =>
  1190. result.UserIsActive = principal.IsActive(),
  1191. [UserLastBadPasswordAttempt] = () =>
  1192. result.UserLastBadPasswordAttempt =
  1193. principal.LastBadPasswordAttempt,
  1194. [UserLastLogon] = () =>
  1195. result.UserLastLogon = principal.LastLogon,
  1196. [UserLastPasswordSet] = () =>
  1197. result.UserLastPasswordSet = principal.LastPasswordSet,
  1198. [UserManager] = () =>
  1199. result.UserManager = principal.GetManager(),
  1200. [UserMiddleName] = () =>
  1201. result.UserMiddleName = principal.MiddleName,
  1202. [UserMobile] = () =>
  1203. result.UserMobile = principal.GetMobile(),
  1204. [UserName] = () =>
  1205. result.UserName = principal.Name,
  1206. [UserNotes] = () =>
  1207. result.UserNotes = principal.GetNotes(),
  1208. [UserPager] = () =>
  1209. result.UserPager = principal.GetPager(),
  1210. [UserPasswordNeverExpires] = () =>
  1211. result.UserPasswordNeverExpires =
  1212. principal.PasswordNeverExpires,
  1213. [UserPasswordNotRequired] = () =>
  1214. result.UserPasswordNotRequired =
  1215. principal.PasswordNotRequired,
  1216. [UserPermittedLogonTimes] = () =>
  1217. result.UserPermittedLogonTimes =
  1218. principal.PermittedLogonTimes,
  1219. [UserPermittedWorkstations] = () =>
  1220. result.UserPermittedWorkstations =
  1221. principal.PermittedWorkstations,
  1222. [UserSamAccountName] = () =>
  1223. result.UserSamAccountName = principal.SamAccountName,
  1224. [UserScriptPath] = () =>
  1225. result.UserScriptPath = principal.ScriptPath,
  1226. [UserSid] = () =>
  1227. result.UserSid = principal.Sid,
  1228. [UserSip] = () =>
  1229. result.UserSip = principal.GetSip(),
  1230. [UserSmartcardLogonRequired] = () =>
  1231. result.UserSmartcardLogonRequired =
  1232. principal.SmartcardLogonRequired,
  1233. [UserState] = () =>
  1234. result.UserState = principal.GetState(),
  1235. [UserStreetAddress] = () =>
  1236. result.UserStreetAddress =
  1237. principal.GetStreetAddress(),
  1238. [UserStructuralObjectClass] = () =>
  1239. result.UserStructuralObjectClass =
  1240. principal.StructuralObjectClass,
  1241. [UserSurname] = () =>
  1242. result.UserSurname = principal.Surname,
  1243. [UserTitle] = () =>
  1244. result.UserTitle = principal.GetTitle(),
  1245. [UserUserCannotChangePassword] = () =>
  1246. result.UserUserCannotChangePassword =
  1247. principal.UserCannotChangePassword,
  1248. [UserUserPrincipalName] = () =>
  1249. result.UserUserPrincipalName =
  1250. principal.UserPrincipalName,
  1251. [UserVoiceTelephoneNumber] = () =>
  1252. result.UserVoiceTelephoneNumber =
  1253. principal.VoiceTelephoneNumber,
  1254. [UserVoip] = () =>
  1255. result.UserVoip = principal.GetVoip()
  1256. };
  1257.  
  1258. if (!attributeMapping.ContainsKey(attribute)) return;
  1259. attributeMapping[attribute]();
  1260. }
  1261.  
  1262. private void AddAttributesToResult(
  1263. ComputerPrincipal computerPrincipal,
  1264. GroupPrincipal groupPrincipal,
  1265. UserPrincipal directReportUserPrincipal,
  1266. UserPrincipal userPrincipal,
  1267. dynamic result)
  1268. {
  1269. foreach (var attribute in Attributes)
  1270. {
  1271. if (computerPrincipal != null)
  1272. {
  1273. if (AddComputerAttributeToResult(
  1274. computerPrincipal, attribute, result))
  1275. {
  1276. continue;
  1277. }
  1278. }
  1279. if (directReportUserPrincipal != null)
  1280. {
  1281. if (AddDirectReportAttributeToResult(
  1282. directReportUserPrincipal, attribute, result))
  1283. {
  1284. continue;
  1285. }
  1286. }
  1287. if (groupPrincipal != null)
  1288. {
  1289. if (AddGroupAttributeToResult(
  1290. groupPrincipal, attribute, result))
  1291. {
  1292. continue;
  1293. }
  1294. }
  1295. if (userPrincipal != null)
  1296. {
  1297. AddUserAttributeToResult(userPrincipal, attribute, result);
  1298. }
  1299. }
  1300. }
  1301. }
  1302. }
  1303.  
  1304. using System;
  1305.  
  1306. namespace ActiveDirectoryToolWpf
  1307. {
  1308. internal class OrganizationalUnit
  1309. {
  1310. private const char Comma = ',';
  1311. private const string DomainComponentPrefix = ",DC=";
  1312. private const string LdapProtocol = "LDAP://";
  1313. private const string OrganizationalUnitPrefix = "OU=";
  1314. private const int StringStartIndex = 0;
  1315.  
  1316. internal string Path { get; set; }
  1317.  
  1318. internal string[] Split
  1319. {
  1320. get
  1321. {
  1322. var namesOnly = Path.Replace(LdapProtocol, string.Empty)
  1323. .Replace(OrganizationalUnitPrefix, string.Empty);
  1324. var firstDomainComponentIndex = namesOnly.IndexOf(
  1325. DomainComponentPrefix, StringComparison.Ordinal);
  1326. namesOnly = namesOnly.Substring(
  1327. StringStartIndex, firstDomainComponentIndex);
  1328. var split = namesOnly.Split(Comma);
  1329. Array.Reverse(split);
  1330. return split;
  1331. }
  1332. }
  1333. }
  1334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement