Advertisement
EmilySamantha80

Full featured Active Directory client in C# ADComputer.cs

Dec 8th, 2016
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 37.66 KB | None | 0 0
  1. // Title:  Full featured Active Directory client in C#
  2. // Author: Emily Heiner
  3. //
  4. // MIT License
  5. // Copyright(c) 2017 Emily Heiner (emilysamantha80@gmail.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. using System;
  25. using System.Collections.Generic;
  26. using System.DirectoryServices;
  27. using System.DirectoryServices.AccountManagement;
  28. using System.Linq;
  29.  
  30. /// <summary>
  31. /// Full featured C# Active Directory client
  32. /// </summary>
  33. namespace ESH.Utility.ActiveDirectory
  34. {
  35.     /// <summary>
  36.     /// Encapsulates principals that are computer accounts
  37.     /// </summary>
  38.     [DirectoryObjectClass("computer")]
  39.     [DirectoryRdnPrefix("CN")]
  40.     public class ADComputerPrincipal : ComputerPrincipal
  41.     {
  42.         /// <summary>
  43.         /// Initializes a new instance of the ADComputerPrincipal class by using the specified context
  44.         /// </summary>
  45.         /// <param name="context">The System.DirectoryService.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  46.         public ADComputerPrincipal(PrincipalContext context)
  47.             : base(context) { }
  48.  
  49.         /// <summary>
  50.         /// Initializes a new instance of the ADComputerPrincipal class by using the specified context, SAM account name, password, and enabled value
  51.         /// </summary>
  52.         /// <param name="context">The System.DirectoryService.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  53.         /// <param name="samAccountName">The SAM account name for this computer principal.</param>
  54.         /// <param name="password">The password for this account.</param>
  55.         /// <param name="enabled">A Boolean value that specifies whether the account is enabled.</param>
  56.         public ADComputerPrincipal(PrincipalContext context, string samAccountName, string password, bool enabled)
  57.             : base(context, samAccountName, password, enabled) { }
  58.  
  59.         private ADComputerPrincipalSearchFilter _AdvancedSearchFilter;
  60.         new public ADComputerPrincipalSearchFilter AdvancedSearchFilter
  61.         {
  62.             get
  63.             {
  64.                 if (_AdvancedSearchFilter == null)
  65.                 {
  66.                     _AdvancedSearchFilter = new ADComputerPrincipalSearchFilter(this);
  67.                 }
  68.                 return _AdvancedSearchFilter;
  69.             }
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Check if the account expires
  74.         /// </summary>
  75.         /// <returns>Returns true if the account expires</returns>
  76.         public bool AccountExpires
  77.         {
  78.             get
  79.             {
  80.                 if (AccountExpirationDate.HasValue == false)
  81.                 {
  82.                     return false;
  83.                 }
  84.                 else
  85.                 {
  86.                     return true;
  87.                 }
  88.             }
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Check if the user account is expired
  93.         /// </summary>
  94.         /// <returns>Returns true if expired</returns>
  95.         public bool IsExpired
  96.         {
  97.             get
  98.             {
  99.                 //Copy the account expiration date into a nullable DateTime variable.
  100.                 DateTime? accountExpiration = this.AccountExpirationDate;
  101.  
  102.                 //Check for null first. Note that the AccountExpirationDate property is null when
  103.                 //it finds 0x0 or 0x7FFFFFFFFFFFFFFF in Active Directory. These values mean that
  104.                 //the account never expires.
  105.                 if (accountExpiration.HasValue == false)
  106.                 {
  107.                     //Expiration date is not set and account never expires.
  108.                     return false;
  109.                 }
  110.                 else
  111.                 {
  112.                     //Compare the value the same way AD Users and Computers does (chop off the time).
  113.                     //Example: Expiration of "08/08/2010 2:00:00 AM" will be represented as:
  114.                     //         "End Of: 08/07/2010", which is the same as "08/08/2010 12:00:00 AM"
  115.                     if (accountExpiration.Value.Date < DateTime.Now)
  116.                     {
  117.                         //Expiration date is set and account has expired.
  118.                         return true;
  119.                     }
  120.                     else
  121.                     {
  122.                         //Expiration date is set but account has not expired yet.
  123.                         return false;
  124.                     }
  125.                 }
  126.  
  127.             }
  128.         }
  129.  
  130.         /// <summary>
  131.         /// Gets or Sets the allow dial-in property
  132.         /// </summary>
  133.         /// <returns>Returns true if the account is set to allow dial-in</returns>
  134.         [DirectoryProperty("msNPAllowDialin")]
  135.         public bool AllowDialIn
  136.         {
  137.             get
  138.             {
  139.                 if (ExtensionGet("msNPAllowDialin").Length == 0)
  140.                 {
  141.                     return false;
  142.                 }
  143.                 return Convert.ToBoolean(ExtensionGet("msNPAllowDialin")[0]);
  144.             }
  145.             set
  146.             {
  147.                 ExtensionSet("msNPAllowDialin", value);
  148.                 if (value)
  149.                     ExtensionSet("userParameters", "m:                    d                         ");
  150.                 else
  151.                     ExtensionSet("userParameters", null);
  152.             }
  153.         }
  154.  
  155.         /// <summary>
  156.         /// Gets the date and time the object was created
  157.         /// </summary>
  158.         /// <returns>Returns a nullable DateTime containing the date and time the object was created</returns>
  159.         [DirectoryProperty("whenCreated")]
  160.         public DateTime? WhenCreated
  161.         {
  162.             get
  163.             {
  164.                 if (ExtensionGet("whenCreated").Length == 0)
  165.                 {
  166.                     return null;
  167.                 }
  168.                 return Convert.ToDateTime(ExtensionGet("whenCreated")[0]).ToLocalTime();
  169.             }
  170.         }
  171.  
  172.         /// <summary>
  173.         /// Gets the date and time the object was modified
  174.         /// </summary>
  175.         /// <returns>Returns a nullable DateTime containing the date and time the object was modified</returns>
  176.         [DirectoryProperty("whenChanged")]
  177.         public DateTime? WhenChanged
  178.         {
  179.             get
  180.             {
  181.                 if (ExtensionGet("whenChanged").Length == 0)
  182.                 {
  183.                     return null;
  184.                 }
  185.                 return Convert.ToDateTime(ExtensionGet("whenChanged")[0]).ToLocalTime();
  186.             }
  187.         }
  188.  
  189.         /// <summary>
  190.         /// Gets or sets a Nullable System.DateTime that specifies the date and time that the account expires
  191.         /// </summary>
  192.         /// <returns>a System.DateTime that specifies the date and time that the account expires, or null if the account never expires</returns>
  193.         [DirectoryProperty("accountExpires")]
  194.         public new DateTime? AccountExpirationDate
  195.         {
  196.             get
  197.             {
  198.                 if (!base.AccountExpirationDate.HasValue)
  199.                 {
  200.                     return null;
  201.                 }
  202.                 return base.AccountExpirationDate.Value.ToLocalTime();
  203.             }
  204.             set
  205.             {
  206.                 if (value.HasValue)
  207.                 {
  208.                     base.AccountExpirationDate = value.Value.ToUniversalTime();
  209.                 }
  210.                 else
  211.                 {
  212.                     base.AccountExpirationDate = value;
  213.                 }
  214.             }
  215.         }
  216.  
  217.         /// <summary>
  218.         /// Gets the Nullable System.DateTime that specifies the date and time that the account was locked out
  219.         /// </summary>
  220.         /// <returns>A System.DateTime that specifies the date and time that the account was locked out, or null if no lockout time is set on the account</returns>
  221.         [DirectoryProperty("lockoutTime")]
  222.         public new DateTime? AccountLockoutTime
  223.         {
  224.             get
  225.             {
  226.                 if (!base.AccountLockoutTime.HasValue)
  227.                 {
  228.                     return null;
  229.                 }
  230.                 return base.AccountLockoutTime.Value.ToLocalTime();
  231.             }
  232.         }
  233.  
  234.         /// <summary>
  235.         /// Gets the Nullable System.DateTime that specifies the date and time of the last incorrect password attempt on this account
  236.         /// </summary>
  237.         /// <returns>A Nullable System.DateTime that specifies the date and time of the last incorrect password attempt on this account, or null if no incorrect password tries are recorded</returns>
  238.         [DirectoryProperty("badPasswordTime")]
  239.         public new DateTime? LastBadPasswordAttempt
  240.         {
  241.             get
  242.             {
  243.                 if (!base.LastBadPasswordAttempt.HasValue)
  244.                 {
  245.                     return null;
  246.                 }
  247.                 return base.LastBadPasswordAttempt.Value.ToLocalTime();
  248.             }
  249.         }
  250.  
  251.         /// <summary>
  252.         /// Gets the Nullable System.DateTime that specifies the date and time of the last logon for this account.
  253.         /// NOTE: This value is guaranteed to be synched across domain controllers at least every 14 days, which means this value is only accurate to within 14 days.
  254.         /// </summary>
  255.         /// <returns>A Nullable System.DateTime that specifies the date and time of the last logon for this account</returns>
  256.         [DirectoryProperty("lastLogon")]
  257.         public new DateTime? LastLogon
  258.         {
  259.             get
  260.             {
  261.                 if (!base.LastLogon.HasValue)
  262.                 {
  263.                     return null;
  264.                 }
  265.                 return base.LastLogon.Value.ToLocalTime();
  266.             }
  267.         }
  268.  
  269.         /// <summary>
  270.         /// Gets the Nullable System.DateTime that specifies the last date and time that the password was set for this account.
  271.         /// </summary>
  272.         /// <returns>A Nullable System.DateTime that specifies the last date and time that the password was set for this account</returns>
  273.         [DirectoryProperty("pwdLastSet")]
  274.         public new DateTime? LastPasswordSet
  275.         {
  276.             get
  277.             {
  278.                 if (!base.LastPasswordSet.HasValue)
  279.                 {
  280.                     return null;
  281.                 }
  282.                 return base.LastPasswordSet.Value.ToLocalTime();
  283.             }
  284.         }
  285.  
  286.         /// <summary>
  287.         /// The operating system name string
  288.         /// </summary>
  289.         /// <returns>A string containing the operating system version, or null if the attribute does not exist</returns>
  290.  
  291.         [DirectoryProperty("operatingSystem")]
  292.         public string OperatingSystem
  293.         {
  294.             get
  295.             {
  296.                 if (ExtensionGet("operatingSystem").Length == 0)
  297.                 {
  298.                     return null;
  299.                 }
  300.                 return Convert.ToString(ExtensionGet("operatingSystem")[0]);
  301.             }
  302.             set
  303.             {
  304.                 ExtensionSet("operatingSystem", value);
  305.             }
  306.         }
  307.  
  308.         /// <summary>
  309.         /// The operating system service pack string
  310.         /// </summary>
  311.         /// <returns>A string containing the operating system service pack, or null if the attribute does not exist</returns>
  312.         [DirectoryProperty("operatingSystemServicePack")]
  313.         public string OperatingSystemServicePack
  314.         {
  315.             get
  316.             {
  317.                 if (ExtensionGet("operatingSystemServicePack").Length == 0)
  318.                 {
  319.                     return null;
  320.                 }
  321.                 return Convert.ToString(ExtensionGet("operatingSystemServicePack")[0]);
  322.             }
  323.             set
  324.             {
  325.                 ExtensionSet("operatingSystemServicePack", value);
  326.             }
  327.         }
  328.  
  329.         /// <summary>
  330.         /// The operating system version string
  331.         /// </summary>
  332.         /// <returns>A string containing the operating system version, or null if the attribute does not exist</returns>
  333.         [DirectoryProperty("operatingSystemVersion")]
  334.         public string OperatingSystemVersion
  335.         {
  336.             get
  337.             {
  338.                 if (ExtensionGet("operatingSystemVersion").Length == 0)
  339.                 {
  340.                     return null;
  341.                 }
  342.                 return Convert.ToString(ExtensionGet("operatingSystemVersion")[0]);
  343.             }
  344.             set
  345.             {
  346.                 ExtensionSet("operatingSystemVersion", value);
  347.             }
  348.         }
  349.  
  350.         /// <summary>
  351.         /// The name of the computer as registered in DNS.
  352.         /// </summary>
  353.         /// <returns>A string containing the dns name of the computer, or null if the attribute does not exist</returns>
  354.         [DirectoryProperty("dNSHostName")]
  355.         public string DnsHostName
  356.         {
  357.             get
  358.             {
  359.                 if (ExtensionGet("dNSHostName").Length == 0)
  360.                 {
  361.                     return null;
  362.                 }
  363.                 return Convert.ToString(ExtensionGet("dNSHostName")[0]);
  364.             }
  365.             set
  366.             {
  367.                 ExtensionSet("dNSHostName", value);
  368.             }
  369.         }
  370.  
  371.         /// <summary>
  372.         /// Extension attribute for Microsoft Exchange.
  373.         /// This attribute can be used for storing arbitrary data about the object.
  374.         /// </summary>
  375.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  376.         [DirectoryProperty("extensionAttribute1")]
  377.         public string ExtensionAttribute1
  378.         {
  379.             get
  380.             {
  381.                 if (ExtensionGet("extensionAttribute1").Length == 0)
  382.                 {
  383.                     return null;
  384.                 }
  385.                 return Convert.ToString(ExtensionGet("extensionAttribute1")[0]);
  386.             }
  387.             set
  388.             {
  389.                 ExtensionSet("extensionAttribute1", value);
  390.             }
  391.         }
  392.  
  393.         /// <summary>
  394.         /// Extension attribute for Microsoft Exchange.
  395.         /// This attribute can be used for storing arbitrary data about the object.
  396.         /// </summary>
  397.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  398.         [DirectoryProperty("extensionAttribute2")]
  399.         public string ExtensionAttribute2
  400.         {
  401.             get
  402.             {
  403.                 if (ExtensionGet("extensionAttribute2").Length == 0)
  404.                 {
  405.                     return null;
  406.                 }
  407.                 return Convert.ToString(ExtensionGet("extensionAttribute2")[0]);
  408.             }
  409.             set
  410.             {
  411.                 ExtensionSet("extensionAttribute2", value);
  412.             }
  413.         }
  414.  
  415.         /// <summary>
  416.         /// Extension attribute for Microsoft Exchange.
  417.         /// This attribute can be used for storing arbitrary data about the object.
  418.         /// </summary>
  419.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  420.         [DirectoryProperty("extensionAttribute3")]
  421.         public string ExtensionAttribute3
  422.         {
  423.             get
  424.             {
  425.                 if (ExtensionGet("extensionAttribute3").Length == 0)
  426.                 {
  427.                     return null;
  428.                 }
  429.                 return Convert.ToString(ExtensionGet("extensionAttribute3")[0]);
  430.             }
  431.             set
  432.             {
  433.                 ExtensionSet("extensionAttribute3", value);
  434.             }
  435.         }
  436.  
  437.         /// <summary>
  438.         /// Extension attribute for Microsoft Exchange.
  439.         /// This attribute can be used for storing arbitrary data about the object.
  440.         /// </summary>
  441.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  442.         [DirectoryProperty("extensionAttribute4")]
  443.         public string ExtensionAttribute4
  444.         {
  445.             get
  446.             {
  447.                 if (ExtensionGet("extensionAttribute4").Length == 0)
  448.                 {
  449.                     return null;
  450.                 }
  451.                 return Convert.ToString(ExtensionGet("extensionAttribute4")[0]);
  452.             }
  453.             set
  454.             {
  455.                 ExtensionSet("extensionAttribute4", value);
  456.             }
  457.         }
  458.  
  459.         /// <summary>
  460.         /// Extension attribute for Microsoft Exchange.
  461.         /// This attribute can be used for storing arbitrary data about the object.
  462.         /// </summary>
  463.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  464.         [DirectoryProperty("extensionAttribute5")]
  465.         public string ExtensionAttribute5
  466.         {
  467.             get
  468.             {
  469.                 if (ExtensionGet("extensionAttribute5").Length == 0)
  470.                 {
  471.                     return null;
  472.                 }
  473.                 return Convert.ToString(ExtensionGet("extensionAttribute5")[0]);
  474.             }
  475.             set
  476.             {
  477.                 ExtensionSet("extensionAttribute5", value);
  478.             }
  479.         }
  480.  
  481.         /// <summary>
  482.         /// Extension attribute for Microsoft Exchange.
  483.         /// This attribute can be used for storing arbitrary data about the object.
  484.         /// </summary>
  485.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  486.         [DirectoryProperty("extensionAttribute6")]
  487.         public string ExtensionAttribute6
  488.         {
  489.             get
  490.             {
  491.                 if (ExtensionGet("extensionAttribute6").Length == 0)
  492.                 {
  493.                     return null;
  494.                 }
  495.                 return Convert.ToString(ExtensionGet("extensionAttribute6")[0]);
  496.             }
  497.             set
  498.             {
  499.                 ExtensionSet("extensionAttribute6", value);
  500.             }
  501.         }
  502.  
  503.         /// <summary>
  504.         /// Extension attribute for Microsoft Exchange.
  505.         /// This attribute can be used for storing arbitrary data about the object.
  506.         /// </summary>
  507.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  508.         [DirectoryProperty("extensionAttribute7")]
  509.         public string ExtensionAttribute7
  510.         {
  511.             get
  512.             {
  513.                 if (ExtensionGet("extensionAttribute7").Length == 0)
  514.                 {
  515.                     return null;
  516.                 }
  517.                 return Convert.ToString(ExtensionGet("extensionAttribute7")[0]);
  518.             }
  519.             set
  520.             {
  521.                 ExtensionSet("extensionAttribute7", value);
  522.             }
  523.         }
  524.  
  525.         /// <summary>
  526.         /// Extension attribute for Microsoft Exchange.
  527.         /// This attribute can be used for storing arbitrary data about the object.
  528.         /// </summary>
  529.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  530.         [DirectoryProperty("extensionAttribute8")]
  531.         public string ExtensionAttribute8
  532.         {
  533.             get
  534.             {
  535.                 if (ExtensionGet("extensionAttribute8").Length == 0)
  536.                 {
  537.                     return null;
  538.                 }
  539.                 return Convert.ToString(ExtensionGet("extensionAttribute8")[0]);
  540.             }
  541.             set
  542.             {
  543.                 ExtensionSet("extensionAttribute8", value);
  544.             }
  545.         }
  546.  
  547.         /// <summary>
  548.         /// Extension attribute for Microsoft Exchange.
  549.         /// This attribute can be used for storing arbitrary data about the object.
  550.         /// </summary>
  551.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  552.         [DirectoryProperty("extensionAttribute9")]
  553.         public string ExtensionAttribute9
  554.         {
  555.             get
  556.             {
  557.                 if (ExtensionGet("extensionAttribute9").Length == 0)
  558.                 {
  559.                     return null;
  560.                 }
  561.                 return Convert.ToString(ExtensionGet("extensionAttribute9")[0]);
  562.             }
  563.             set
  564.             {
  565.                 ExtensionSet("extensionAttribute9", value);
  566.             }
  567.         }
  568.  
  569.         /// <summary>
  570.         /// Extension attribute for Microsoft Exchange.
  571.         /// This attribute can be used for storing arbitrary data about the object.
  572.         /// </summary>
  573.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  574.         [DirectoryProperty("extensionAttribute10")]
  575.         public string ExtensionAttribute10
  576.         {
  577.             get
  578.             {
  579.                 if (ExtensionGet("extensionAttribute10").Length == 0)
  580.                 {
  581.                     return null;
  582.                 }
  583.                 return Convert.ToString(ExtensionGet("extensionAttribute10")[0]);
  584.             }
  585.             set
  586.             {
  587.                 ExtensionSet("extensionAttribute10", value);
  588.             }
  589.         }
  590.  
  591.         /// <summary>
  592.         /// Extension attribute for Microsoft Exchange.
  593.         /// This attribute can be used for storing arbitrary data about the object.
  594.         /// </summary>
  595.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  596.         [DirectoryProperty("extensionAttribute11")]
  597.         public string ExtensionAttribute11
  598.         {
  599.             get
  600.             {
  601.                 if (ExtensionGet("extensionAttribute11").Length == 0)
  602.                 {
  603.                     return null;
  604.                 }
  605.                 return Convert.ToString(ExtensionGet("extensionAttribute11")[0]);
  606.             }
  607.             set
  608.             {
  609.                 ExtensionSet("extensionAttribute11", value);
  610.             }
  611.         }
  612.  
  613.         /// <summary>
  614.         /// Extension attribute for Microsoft Exchange.
  615.         /// This attribute can be used for storing arbitrary data about the object.
  616.         /// </summary>
  617.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  618.         [DirectoryProperty("extensionAttribute12")]
  619.         public string ExtensionAttribute12
  620.         {
  621.             get
  622.             {
  623.                 if (ExtensionGet("extensionAttribute12").Length == 0)
  624.                 {
  625.                     return null;
  626.                 }
  627.                 return Convert.ToString(ExtensionGet("extensionAttribute12")[0]);
  628.             }
  629.             set
  630.             {
  631.                 ExtensionSet("extensionAttribute12", value);
  632.             }
  633.         }
  634.  
  635.         /// <summary>
  636.         /// Extension attribute for Microsoft Exchange.
  637.         /// This attribute can be used for storing arbitrary data about the object.
  638.         /// </summary>
  639.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  640.         [DirectoryProperty("extensionAttribute13")]
  641.         public string ExtensionAttribute13
  642.         {
  643.             get
  644.             {
  645.                 if (ExtensionGet("extensionAttribute13").Length == 0)
  646.                 {
  647.                     return null;
  648.                 }
  649.                 return Convert.ToString(ExtensionGet("extensionAttribute13")[0]);
  650.             }
  651.             set
  652.             {
  653.                 ExtensionSet("extensionAttribute13", value);
  654.             }
  655.         }
  656.  
  657.         /// <summary>
  658.         /// Extension attribute for Microsoft Exchange.
  659.         /// This attribute can be used for storing arbitrary data about the object.
  660.         /// </summary>
  661.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  662.         [DirectoryProperty("extensionAttribute14")]
  663.         public string ExtensionAttribute14
  664.         {
  665.             get
  666.             {
  667.                 if (ExtensionGet("extensionAttribute14").Length == 0)
  668.                 {
  669.                     return null;
  670.                 }
  671.                 return Convert.ToString(ExtensionGet("extensionAttribute14")[0]);
  672.             }
  673.             set
  674.             {
  675.                 ExtensionSet("extensionAttribute14", value);
  676.             }
  677.         }
  678.  
  679.         /// <summary>
  680.         /// Extension attribute for Microsoft Exchange.
  681.         /// This attribute can be used for storing arbitrary data about the object.
  682.         /// </summary>
  683.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  684.         [DirectoryProperty("extensionAttribute15")]
  685.         public string ExtensionAttribute15
  686.         {
  687.             get
  688.             {
  689.                 if (ExtensionGet("extensionAttribute15").Length == 0)
  690.                 {
  691.                     return null;
  692.                 }
  693.                 return Convert.ToString(ExtensionGet("extensionAttribute15")[0]);
  694.             }
  695.             set
  696.             {
  697.                 ExtensionSet("extensionAttribute15", value);
  698.             }
  699.         }
  700.  
  701.         /// <summary>
  702.         /// Sets the value of the specified attribute.
  703.         /// For multi-valued attributes, pass an array to the parameter 'value'
  704.         /// </summary>
  705.         /// <param name="attribute">The name of the attribute</param>
  706.         /// <param name="value">The value to set</param>
  707.         public void SetAttribute(string attribute, object value)
  708.         {
  709.             ExtensionSet(attribute, value);
  710.         }
  711.  
  712.         /// <summary>
  713.         /// Returns the value of the specified attribute.
  714.         /// If more than one value is in the attribute, the values will be separated by a semicolon
  715.         /// </summary>
  716.         /// <param name="attribute">The name of the attribute</param>
  717.         /// <returns>The attribute value</returns>
  718.         public object[] GetAttribute(string attribute)
  719.         {
  720.             return ExtensionGet(attribute);
  721.         }
  722.  
  723.         /// <summary>
  724.         /// Returns the value of the specified attribute.
  725.         /// If more than one value is in the attribute, the values will be separated by a semicolon
  726.         /// </summary>
  727.         /// <param name="attribute">The name of the attribute to get</param>
  728.         /// <returns>The attribute value</returns>
  729.         public string GetAttributeString(string attribute)
  730.         {
  731.             object[] value = ExtensionGet(attribute);
  732.             if (value.Length == 0)
  733.             {
  734.                 return null;
  735.             }
  736.             string result = String.Empty;
  737.             for (int i = 0; i < value.Length; i++)
  738.             {
  739.                 result += Convert.ToString(ExtensionGet(attribute)[i]) + ";";
  740.             }
  741.             return result.TrimEnd(';');
  742.         }
  743.  
  744.         /// <summary>
  745.         /// Returns all of the Active Directory attributes for the specified user.
  746.         /// Multi-valued attributes will be separated by a semicolon.
  747.         /// </summary>
  748.         /// <returns>Dictionary object containing the name/value pairs for the user properties</returns>
  749.         public IDictionary<string, string> GetAttributes()
  750.         {
  751.             var propertyList = new Dictionary<string, string>();
  752.  
  753.             DirectoryEntry dirEntry = this.GetUnderlyingObject() as DirectoryEntry;
  754.             PropertyCollection properties = dirEntry.Properties;
  755.  
  756.             foreach (string propertyName in properties.PropertyNames)
  757.             {
  758.                 string result = String.Empty;
  759.                 PropertyValueCollection property = properties[propertyName];
  760.  
  761.                 if (property != null && property.Count > 0)
  762.                 {
  763.                     foreach (object value in property)
  764.                     {
  765.                         if (result == String.Empty)
  766.                         {
  767.                             result = "";
  768.                         }
  769.                         else
  770.                         {
  771.                             result += ";";
  772.                         }
  773.                         result += value.ToString();
  774.                     }
  775.                 }
  776.                 else
  777.                 {
  778.                     result = String.Empty;
  779.                 }
  780.                 propertyList.Add(propertyName, result);
  781.             }
  782.  
  783.             dirEntry.Dispose();
  784.             return propertyList;
  785.         }
  786.  
  787.         /// <summary>
  788.         /// Returns the ADComputerPrincipal object that matches the specified identity value.
  789.         /// </summary>
  790.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  791.         /// <param name="identityValue">The identity of the principal.</param>
  792.         /// <returns>An ADComputerPrincipal object that matches the specified identity value or null if no matches are found</returns>
  793.         public static new ADComputerPrincipal FindByIdentity(PrincipalContext principalContext, string identityValue)
  794.         {
  795.             return (ADComputerPrincipal)FindByIdentityWithType(principalContext, typeof(ADComputerPrincipal), identityValue);
  796.         }
  797.  
  798.         /// <summary>
  799.         /// Returns the ADComputerPrincipal object that matches the specified identity type, and value.
  800.         /// </summary>
  801.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  802.         /// <param name="identityType">An System.DirectoryServices.AccountManagement.IdentityType enumeration value that specifies the type of the identity value.</param>
  803.         /// <param name="identityValue">The identity of the principal.</param>
  804.         /// <returns>An ADComputerPrincipal object that matches the specified identity value or null if no matches are found</returns>
  805.         public static new ADComputerPrincipal FindByIdentity(PrincipalContext principalContext, IdentityType identityType, string identityValue)
  806.         {
  807.             return (ADComputerPrincipal)FindByIdentityWithType(principalContext, typeof(ADComputerPrincipal), identityType, identityValue);
  808.         }
  809.  
  810.         /// <summary>
  811.         /// Returns the ADComputerPrincipal objects that match the specified attribute value and match type
  812.         /// </summary>
  813.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  814.         /// <param name="attribute">The attribute name to be queried.</param>
  815.         /// <param name="value">The attribute value to match.</param>
  816.         /// <param name="mt">A System.DirectoryServices.AccountManagement.MatchType object that specifies the type of comparison.</param>
  817.         /// <returns>A List of type ADComputerPrincipal that matches the specified attribute value.</returns>
  818.         public static List<ADComputerPrincipal> FindByAttribute(PrincipalContext principalContext, string attribute, string value, MatchType mt)
  819.         {
  820.             ADComputerPrincipal filter = new ADComputerPrincipal(principalContext);
  821.             filter.AdvancedSearchFilter.FindByAttribute(attribute, value, typeof(string), mt);
  822.             PrincipalSearcher ps = new PrincipalSearcher(filter);
  823.             return ps.FindAll().Cast<ADComputerPrincipal>().ToList();
  824.         }
  825.  
  826.         /// <summary>
  827.         /// Returns the ADComputerPrincipal objects that match the specified attribute value
  828.         /// </summary>
  829.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  830.         /// <param name="attribute">The attribute name to be queried.</param>
  831.         /// <param name="value">The attribute value to match.</param>
  832.         /// <returns>A List of type ADComputerPrincipal that matches the specified attribute value.</returns>
  833.         public static List<ADComputerPrincipal> FindByAttribute(PrincipalContext principalContext, string attribute, string value)
  834.         {
  835.             return FindByAttribute(principalContext, attribute, value, MatchType.Equals);
  836.         }
  837.  
  838.         /// <summary>
  839.         /// Returns the ADComputerPrincipal object that matches the specified attribute value and match type
  840.         /// </summary>
  841.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  842.         /// <param name="attribute">The attribute name to be queried.</param>
  843.         /// <param name="value">The attribute value to match.</param>
  844.         /// <param name="mt">A System.DirectoryServices.AccountManagement.MatchType object that specifies the type of comparison.</param>
  845.         /// <returns>An ADComputerPrincipal object that matches the specified attribute value or null if no match is found.</returns>
  846.         public static ADComputerPrincipal FindOneByAttribute(PrincipalContext principalContext, string attribute, string value, MatchType mt)
  847.         {
  848.             var filter = new ADComputerPrincipal(principalContext);
  849.             filter.AdvancedSearchFilter.FindByAttribute(attribute, value, typeof(string), mt);
  850.             PrincipalSearcher ps = new PrincipalSearcher(filter);
  851.             return (ADComputerPrincipal)ps.FindOne();
  852.         }
  853.  
  854.         /// <summary>
  855.         /// Returns the ADComputerPrincipal object that matches the specified attribute value and match type
  856.         /// </summary>
  857.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  858.         /// <param name="attribute">The attribute name to be queried.</param>
  859.         /// <param name="value">The attribute value to match.</param>
  860.         /// <param name="mt">A System.DirectoryServices.AccountManagement.MatchType object that specifies the type of comparison.</param>
  861.         /// <returns>An ADComputerPrincipal object that matches the specified attribute value or null if no match is found.</returns>
  862.         public static ADComputerPrincipal FindOneByAttribute(PrincipalContext principalContext, string attribute, string value)
  863.         {
  864.             return FindOneByAttribute(principalContext, attribute, value, MatchType.Equals);
  865.         }
  866.  
  867.         /// <summary>
  868.         /// Returns a List of ADComputerPrincipal objects that matches the values specified in the principal object
  869.         /// </summary>
  870.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  871.         /// <param name="ADUserPrincipal">The ADComputerPrincipal object to be matched from.</param>
  872.         /// <returns>A List of type ADComputerPrincipal containing the objects that match the specified principal values.</returns>
  873.         public static List<ADComputerPrincipal> FindByPrincipalSearcher(PrincipalContext principalContext, ADComputerPrincipal principal)
  874.         {
  875.             var results = new List<ADComputerPrincipal>();
  876.  
  877.             PrincipalSearcher ps = new PrincipalSearcher(principal);
  878.             foreach (ADComputerPrincipal p in ps.FindAll())
  879.             {
  880.                 results.Add(p);
  881.             }
  882.             return results;
  883.         }
  884.  
  885.         /// <summary>
  886.         /// Returns the ADComputerPrincipal objects that matches the values specified in the principal object
  887.         /// </summary>
  888.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  889.         /// <param name="ADUserPrincipal">The ADComputerPrincipal object to be matched from.</param>
  890.         /// <returns>An ADComputerPrincipal objects that match the specified principal values or null if no match is found.</returns>
  891.         public static ADComputerPrincipal FindOneByPrincipalSearcher(PrincipalContext principalContext, ADComputerPrincipal principal)
  892.         {
  893.             PrincipalSearcher ps = new PrincipalSearcher(principal);
  894.             return (ADComputerPrincipal)ps.FindOne();
  895.         }
  896.  
  897.     }
  898.  
  899.     public class ADComputerPrincipalSearchFilter : AdvancedFilters
  900.     {
  901.         public ADComputerPrincipalSearchFilter(Principal p) : base(p) { }
  902.         public void ExtensionAttribute10(string value, MatchType mt)
  903.         {
  904.             this.AdvancedFilterSet("extensionAttribute10", value, typeof(string), mt);
  905.         }
  906.         public void FindByAttribute(string attribute, string value, Type objectType, MatchType mt)
  907.         {
  908.             this.AdvancedFilterSet(attribute, value, objectType, mt);
  909.         }
  910.     }
  911. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement