Advertisement
EmilySamantha80

Full featured Active Directory client in C# ADGroup.cs

Dec 8th, 2016
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 31.12 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.  
  25. using System;
  26. using System.Collections.Generic;
  27. using System.DirectoryServices;
  28. using System.DirectoryServices.AccountManagement;
  29. using System.Linq;
  30.  
  31. /// <summary>
  32. /// Full featured C# Active Directory client
  33. /// </summary>
  34. namespace ESH.Utility.ActiveDirectory
  35. {
  36.     /// <summary>
  37.     /// Encapsulates group accounts. Group accounts can be arbitrary collections of principal objects or accounts created for administrative purposes.
  38.     /// </summary>
  39.     [DirectoryObjectClass("group")]
  40.     [DirectoryRdnPrefix("CN")]
  41.     public class ADGroupPrincipal : GroupPrincipal
  42.     {
  43.         /// <summary>
  44.         /// Initializes a new instance of the System.DirectoryServices.AccountManagement.GroupPrincipal class and assigns it to the specified context.
  45.         /// </summary>
  46.         /// <param name="context">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  47.         public ADGroupPrincipal(PrincipalContext context)
  48.             : base(context) { }
  49.  
  50.         /// <summary>
  51.         /// Initializes a new instance of the System.DirectoryServices.AccountManagement.GroupPrincipal class and assigns it to the specified context and SAM account name.
  52.         /// </summary>
  53.         /// <param name="context">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  54.         /// <param name="samAccountName">The SAM account name for this principal.</param>
  55.         public ADGroupPrincipal(PrincipalContext context, string samAccountName)
  56.             : base(context, samAccountName) { }
  57.  
  58.         private ADGroupPrincipalSearchFilter _AdvancedSearchFilter;
  59.         public ADGroupPrincipalSearchFilter AdvancedSearchFilter
  60.         {
  61.             get
  62.             {
  63.                 if (_AdvancedSearchFilter == null)
  64.                 {
  65.                     _AdvancedSearchFilter = new ADGroupPrincipalSearchFilter(this);
  66.                 }
  67.                 return _AdvancedSearchFilter;
  68.             }
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Gets the date and time the object was created
  73.         /// </summary>
  74.         /// <returns>Returns a nullable DateTime containing the date and time the object was created</returns>
  75.         [DirectoryProperty("whenCreated")]
  76.         public DateTime? WhenCreated
  77.         {
  78.             get
  79.             {
  80.                 if (ExtensionGet("whenCreated").Length == 0)
  81.                 {
  82.                     return null;
  83.                 }
  84.                 return Convert.ToDateTime(ExtensionGet("whenCreated")[0]).ToLocalTime();
  85.             }
  86.         }
  87.  
  88.         /// <summary>
  89.         /// Gets the date and time the object was modified
  90.         /// </summary>
  91.         /// <returns>Returns a nullable DateTime containing the date and time the object was modified</returns>
  92.         [DirectoryProperty("whenChanged")]
  93.         public DateTime? WhenChanged
  94.         {
  95.             get
  96.             {
  97.                 if (ExtensionGet("whenChanged").Length == 0)
  98.                 {
  99.                     return null;
  100.                 }
  101.                 return Convert.ToDateTime(ExtensionGet("whenChanged")[0]).ToLocalTime();
  102.             }
  103.         }
  104.  
  105.         /// <summary>
  106.         ///  Specifies the delivery address to which e-mail for this recipient should be sent.
  107.         /// </summary>
  108.         /// <returns>A string containing the address</returns>
  109.         [DirectoryProperty("targetAddress")]
  110.         public string TargetAddress
  111.         {
  112.             get
  113.             {
  114.                 if (ExtensionGet("targetAddress").Length == 0)
  115.                 {
  116.                     return null;
  117.                 }
  118.                 return Convert.ToString(ExtensionGet("targetAddress")[0]);
  119.             }
  120.             set
  121.             {
  122.                 ExtensionSet("targetAddress", value);
  123.             }
  124.         }
  125.  
  126.         /// <summary>
  127.         /// A proxy address is the address by which a Microsoft Exchange Server recipient object is recognized in a foreign mail system.
  128.         /// Proxy addresses are required for all recipient objects, such as custom recipients and distribution lists.
  129.         /// </summary>
  130.         /// <returns>An array of strings containing the proxy addresses</returns>
  131.         [DirectoryProperty("proxyAddresses")]
  132.         public string[] ProxyAddresses
  133.         {
  134.             get
  135.             {
  136.                 return (string[])GetAttribute("proxyAddresses");
  137.             }
  138.             set
  139.             {
  140.                 ExtensionSet("proxyAddresses", value);
  141.             }
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Whether or not the object is displayed in the Global Address List
  146.         /// </summary>
  147.         /// <returns>True if the object is hidden from the Global Address List</returns>
  148.         [DirectoryProperty("msExchHideFromAddressLists")]
  149.         public bool HideFromAddressLists
  150.         {
  151.             get
  152.             {
  153.                 if (ExtensionGet("msExchHideFromAddressLists").Length == 0)
  154.                 {
  155.                     return false;
  156.                 }
  157.                 return Convert.ToBoolean(ExtensionGet("msExchHideFromAddressLists")[0]);
  158.             }
  159.             set
  160.             {
  161.                 ExtensionSet("msExchHideFromAddressLists", value);
  162.             }
  163.         }
  164.  
  165.         /// <summary>
  166.         /// Extension attribute for Microsoft Exchange.
  167.         /// This attribute can be used for storing arbitrary data about the object.
  168.         /// </summary>
  169.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  170.         [DirectoryProperty("extensionAttribute1")]
  171.         public string ExtensionAttribute1
  172.         {
  173.             get
  174.             {
  175.                 if (ExtensionGet("extensionAttribute1").Length == 0)
  176.                 {
  177.                     return null;
  178.                 }
  179.                 return Convert.ToString(ExtensionGet("extensionAttribute1")[0]);
  180.             }
  181.             set
  182.             {
  183.                 ExtensionSet("extensionAttribute1", value);
  184.             }
  185.         }
  186.  
  187.         /// <summary>
  188.         /// Extension attribute for Microsoft Exchange.
  189.         /// This attribute can be used for storing arbitrary data about the object.
  190.         /// </summary>
  191.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  192.         [DirectoryProperty("extensionAttribute2")]
  193.         public string ExtensionAttribute2
  194.         {
  195.             get
  196.             {
  197.                 if (ExtensionGet("extensionAttribute2").Length == 0)
  198.                 {
  199.                     return null;
  200.                 }
  201.                 return Convert.ToString(ExtensionGet("extensionAttribute2")[0]);
  202.             }
  203.             set
  204.             {
  205.                 ExtensionSet("extensionAttribute2", value);
  206.             }
  207.         }
  208.  
  209.         /// <summary>
  210.         /// Extension attribute for Microsoft Exchange.
  211.         /// This attribute can be used for storing arbitrary data about the object.
  212.         /// </summary>
  213.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  214.         [DirectoryProperty("extensionAttribute3")]
  215.         public string ExtensionAttribute3
  216.         {
  217.             get
  218.             {
  219.                 if (ExtensionGet("extensionAttribute3").Length == 0)
  220.                 {
  221.                     return null;
  222.                 }
  223.                 return Convert.ToString(ExtensionGet("extensionAttribute3")[0]);
  224.             }
  225.             set
  226.             {
  227.                 ExtensionSet("extensionAttribute3", value);
  228.             }
  229.         }
  230.  
  231.         /// <summary>
  232.         /// Extension attribute for Microsoft Exchange.
  233.         /// This attribute can be used for storing arbitrary data about the object.
  234.         /// </summary>
  235.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  236.         [DirectoryProperty("extensionAttribute4")]
  237.         public string ExtensionAttribute4
  238.         {
  239.             get
  240.             {
  241.                 if (ExtensionGet("extensionAttribute4").Length == 0)
  242.                 {
  243.                     return null;
  244.                 }
  245.                 return Convert.ToString(ExtensionGet("extensionAttribute4")[0]);
  246.             }
  247.             set
  248.             {
  249.                 ExtensionSet("extensionAttribute4", value);
  250.             }
  251.         }
  252.  
  253.         /// <summary>
  254.         /// Extension attribute for Microsoft Exchange.
  255.         /// This attribute can be used for storing arbitrary data about the object.
  256.         /// </summary>
  257.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  258.         [DirectoryProperty("extensionAttribute5")]
  259.         public string ExtensionAttribute5
  260.         {
  261.             get
  262.             {
  263.                 if (ExtensionGet("extensionAttribute5").Length == 0)
  264.                 {
  265.                     return null;
  266.                 }
  267.                 return Convert.ToString(ExtensionGet("extensionAttribute5")[0]);
  268.             }
  269.             set
  270.             {
  271.                 ExtensionSet("extensionAttribute5", value);
  272.             }
  273.         }
  274.  
  275.         /// <summary>
  276.         /// Extension attribute for Microsoft Exchange.
  277.         /// This attribute can be used for storing arbitrary data about the object.
  278.         /// </summary>
  279.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  280.         [DirectoryProperty("extensionAttribute6")]
  281.         public string ExtensionAttribute6
  282.         {
  283.             get
  284.             {
  285.                 if (ExtensionGet("extensionAttribute6").Length == 0)
  286.                 {
  287.                     return null;
  288.                 }
  289.                 return Convert.ToString(ExtensionGet("extensionAttribute6")[0]);
  290.             }
  291.             set
  292.             {
  293.                 ExtensionSet("extensionAttribute6", value);
  294.             }
  295.         }
  296.  
  297.         /// <summary>
  298.         /// Extension attribute for Microsoft Exchange.
  299.         /// This attribute can be used for storing arbitrary data about the object.
  300.         /// </summary>
  301.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  302.         [DirectoryProperty("extensionAttribute7")]
  303.         public string ExtensionAttribute7
  304.         {
  305.             get
  306.             {
  307.                 if (ExtensionGet("extensionAttribute7").Length == 0)
  308.                 {
  309.                     return null;
  310.                 }
  311.                 return Convert.ToString(ExtensionGet("extensionAttribute7")[0]);
  312.             }
  313.             set
  314.             {
  315.                 ExtensionSet("extensionAttribute7", value);
  316.             }
  317.         }
  318.  
  319.         /// <summary>
  320.         /// Extension attribute for Microsoft Exchange.
  321.         /// This attribute can be used for storing arbitrary data about the object.
  322.         /// </summary>
  323.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  324.         [DirectoryProperty("extensionAttribute8")]
  325.         public string ExtensionAttribute8
  326.         {
  327.             get
  328.             {
  329.                 if (ExtensionGet("extensionAttribute8").Length == 0)
  330.                 {
  331.                     return null;
  332.                 }
  333.                 return Convert.ToString(ExtensionGet("extensionAttribute8")[0]);
  334.             }
  335.             set
  336.             {
  337.                 ExtensionSet("extensionAttribute8", value);
  338.             }
  339.         }
  340.  
  341.         /// <summary>
  342.         /// Extension attribute for Microsoft Exchange.
  343.         /// This attribute can be used for storing arbitrary data about the object.
  344.         /// </summary>
  345.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  346.         [DirectoryProperty("extensionAttribute9")]
  347.         public string ExtensionAttribute9
  348.         {
  349.             get
  350.             {
  351.                 if (ExtensionGet("extensionAttribute9").Length == 0)
  352.                 {
  353.                     return null;
  354.                 }
  355.                 return Convert.ToString(ExtensionGet("extensionAttribute9")[0]);
  356.             }
  357.             set
  358.             {
  359.                 ExtensionSet("extensionAttribute9", value);
  360.             }
  361.         }
  362.  
  363.         /// <summary>
  364.         /// Extension attribute for Microsoft Exchange.
  365.         /// This attribute can be used for storing arbitrary data about the object.
  366.         /// </summary>
  367.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  368.         [DirectoryProperty("extensionAttribute10")]
  369.         public string ExtensionAttribute10
  370.         {
  371.             get
  372.             {
  373.                 if (ExtensionGet("extensionAttribute10").Length == 0)
  374.                 {
  375.                     return null;
  376.                 }
  377.                 return Convert.ToString(ExtensionGet("extensionAttribute10")[0]);
  378.             }
  379.             set
  380.             {
  381.                 ExtensionSet("extensionAttribute10", value);
  382.             }
  383.         }
  384.  
  385.         /// <summary>
  386.         /// Extension attribute for Microsoft Exchange.
  387.         /// This attribute can be used for storing arbitrary data about the object.
  388.         /// </summary>
  389.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  390.         [DirectoryProperty("extensionAttribute11")]
  391.         public string ExtensionAttribute11
  392.         {
  393.             get
  394.             {
  395.                 if (ExtensionGet("extensionAttribute11").Length == 0)
  396.                 {
  397.                     return null;
  398.                 }
  399.                 return Convert.ToString(ExtensionGet("extensionAttribute11")[0]);
  400.             }
  401.             set
  402.             {
  403.                 ExtensionSet("extensionAttribute11", value);
  404.             }
  405.         }
  406.  
  407.         /// <summary>
  408.         /// Extension attribute for Microsoft Exchange.
  409.         /// This attribute can be used for storing arbitrary data about the object.
  410.         /// </summary>
  411.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  412.         [DirectoryProperty("extensionAttribute12")]
  413.         public string ExtensionAttribute12
  414.         {
  415.             get
  416.             {
  417.                 if (ExtensionGet("extensionAttribute12").Length == 0)
  418.                 {
  419.                     return null;
  420.                 }
  421.                 return Convert.ToString(ExtensionGet("extensionAttribute12")[0]);
  422.             }
  423.             set
  424.             {
  425.                 ExtensionSet("extensionAttribute12", value);
  426.             }
  427.         }
  428.  
  429.         /// <summary>
  430.         /// Extension attribute for Microsoft Exchange.
  431.         /// This attribute can be used for storing arbitrary data about the object.
  432.         /// </summary>
  433.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  434.         [DirectoryProperty("extensionAttribute13")]
  435.         public string ExtensionAttribute13
  436.         {
  437.             get
  438.             {
  439.                 if (ExtensionGet("extensionAttribute13").Length == 0)
  440.                 {
  441.                     return null;
  442.                 }
  443.                 return Convert.ToString(ExtensionGet("extensionAttribute13")[0]);
  444.             }
  445.             set
  446.             {
  447.                 ExtensionSet("extensionAttribute13", value);
  448.             }
  449.         }
  450.  
  451.         /// <summary>
  452.         /// Extension attribute for Microsoft Exchange.
  453.         /// This attribute can be used for storing arbitrary data about the object.
  454.         /// </summary>
  455.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  456.         [DirectoryProperty("extensionAttribute14")]
  457.         public string ExtensionAttribute14
  458.         {
  459.             get
  460.             {
  461.                 if (ExtensionGet("extensionAttribute14").Length == 0)
  462.                 {
  463.                     return null;
  464.                 }
  465.                 return Convert.ToString(ExtensionGet("extensionAttribute14")[0]);
  466.             }
  467.             set
  468.             {
  469.                 ExtensionSet("extensionAttribute14", value);
  470.             }
  471.         }
  472.  
  473.         /// <summary>
  474.         /// Extension attribute for Microsoft Exchange.
  475.         /// This attribute can be used for storing arbitrary data about the object.
  476.         /// </summary>
  477.         /// <returns>A string containing the value of the attribute or null if the attribute does not exist.</returns>
  478.         [DirectoryProperty("extensionAttribute15")]
  479.         public string ExtensionAttribute15
  480.         {
  481.             get
  482.             {
  483.                 if (ExtensionGet("extensionAttribute15").Length == 0)
  484.                 {
  485.                     return null;
  486.                 }
  487.                 return Convert.ToString(ExtensionGet("extensionAttribute15")[0]);
  488.             }
  489.             set
  490.             {
  491.                 ExtensionSet("extensionAttribute15", value);
  492.             }
  493.         }
  494.  
  495.         /// <summary>
  496.         /// Sets the value of the specified attribute.
  497.         /// For multi-valued attributes, pass an array to the parameter 'value'
  498.         /// </summary>
  499.         /// <param name="attribute">The name of the attribute</param>
  500.         /// <param name="value">The value to set</param>
  501.         public void SetAttribute(string attribute, object value)
  502.         {
  503.             ExtensionSet(attribute, value);
  504.         }
  505.  
  506.         /// <summary>
  507.         /// Returns the value of the specified attribute.
  508.         /// If more than one value is in the attribute, the values will be separated by a semicolon
  509.         /// </summary>
  510.         /// <param name="attribute">The name of the attribute</param>
  511.         /// <returns>The attribute value</returns>
  512.         public object[] GetAttribute(string attribute)
  513.         {
  514.             return ExtensionGet(attribute);
  515.         }
  516.  
  517.         /// <summary>
  518.         /// Returns the value of the specified attribute.
  519.         /// If more than one value is in the attribute, the values will be separated by a semicolon
  520.         /// </summary>
  521.         /// <param name="attribute">The name of the attribute to get</param>
  522.         /// <returns>The attribute value</returns>
  523.         public string GetAttributeString(string attribute)
  524.         {
  525.             object[] value = ExtensionGet(attribute);
  526.             if (value.Length == 0)
  527.             {
  528.                 return null;
  529.             }
  530.             string result = String.Empty;
  531.             for (int i = 0; i < value.Length; i++)
  532.             {
  533.                 result += Convert.ToString(ExtensionGet(attribute)[i]) + ";";
  534.             }
  535.             return result.TrimEnd(';');
  536.         }
  537.  
  538.         /// <summary>
  539.         /// Returns all of the Active Directory attributes for the specified user.
  540.         /// Multi-valued attributes will be separated by a semicolon.
  541.         /// </summary>
  542.         /// <returns>Dictionary object containing the name/value pairs for the user properties</returns>
  543.         public IDictionary<string, string> GetAttributes()
  544.         {
  545.             var propertyList = new Dictionary<string, string>();
  546.  
  547.             DirectoryEntry dirEntry = this.GetUnderlyingObject() as DirectoryEntry;
  548.             PropertyCollection properties = dirEntry.Properties;
  549.  
  550.             foreach (string propertyName in properties.PropertyNames)
  551.             {
  552.                 string result = String.Empty;
  553.                 PropertyValueCollection property = properties[propertyName];
  554.  
  555.                 if (property != null && property.Count > 0)
  556.                 {
  557.                     foreach (object value in property)
  558.                     {
  559.                         if (result == String.Empty)
  560.                             result = "";
  561.                         else
  562.                             result += ";";
  563.  
  564.                         result += value.ToString();
  565.                     }
  566.                 }
  567.                 else
  568.                 {
  569.                     result = String.Empty;
  570.                 }
  571.                 propertyList.Add(propertyName, result);
  572.             }
  573.  
  574.             dirEntry.Dispose();
  575.             return propertyList;
  576.         }
  577.  
  578.         /// <summary>
  579.         /// Returns the ADGroupPrincipal object that matches the specified identity value.
  580.         /// </summary>
  581.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  582.         /// <param name="identityValue">The identity of the principal.</param>
  583.         /// <returns>An ADGroupPrincipal object that matches the specified identity value or null if no matches are found</returns>
  584.         public static new ADGroupPrincipal FindByIdentity(PrincipalContext context, string identityValue)
  585.         {
  586.             return (ADGroupPrincipal)FindByIdentityWithType(context, typeof(ADGroupPrincipal), identityValue);
  587.         }
  588.  
  589.         /// <summary>
  590.         /// Returns the AGroupPrincipal object that matches the specified identity type, and value.
  591.         /// </summary>
  592.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  593.         /// <param name="identityType">An System.DirectoryServices.AccountManagement.IdentityType enumeration value that specifies the type of the identity value.</param>
  594.         /// <param name="identityValue">The identity of the principal.</param>
  595.         /// <returns>An ADGroupPrincipal object that matches the specified identity value or null if no matches are found</returns>
  596.         public static new ADGroupPrincipal FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
  597.         {
  598.             return (ADGroupPrincipal)FindByIdentityWithType(context, typeof(ADGroupPrincipal), identityType, identityValue);
  599.         }
  600.  
  601.         /// <summary>
  602.         /// Returns the ADGroupPrincipal objects that match the specified attribute value and match type
  603.         /// </summary>
  604.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  605.         /// <param name="attribute">The attribute name to be queried.</param>
  606.         /// <param name="value">The attribute value to match.</param>
  607.         /// <param name="mt">A System.DirectoryServices.AccountManagement.MatchType object that specifies the type of comparison.</param>
  608.         /// <returns>A List of type ADGroupPrincipal that matches the specified attribute value.</returns>
  609.         public static List<ADGroupPrincipal> FindByAttribute(PrincipalContext context, string attribute, string value)
  610.         {
  611.             return FindByAttribute(context, attribute, value, MatchType.Equals);
  612.         }
  613.  
  614.         /// <summary>
  615.         /// Returns the ADGroupPrincipal objects that match the specified attribute value
  616.         /// </summary>
  617.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  618.         /// <param name="attribute">The attribute name to be queried.</param>
  619.         /// <param name="value">The attribute value to match.</param>
  620.         /// <returns>A List of type ADGroupPrincipal that matches the specified attribute value.</returns>
  621.         public static List<ADGroupPrincipal> FindByAttribute(PrincipalContext context, string attribute, string value, MatchType mt)
  622.         {
  623.             ADGroupPrincipal filter = new ADGroupPrincipal(context);
  624.             filter.AdvancedSearchFilter.FindByAttribute(attribute, value, typeof(string), mt);
  625.             PrincipalSearcher ps = new PrincipalSearcher(filter);
  626.             return ps.FindAll().Cast<ADGroupPrincipal>().ToList();
  627.         }
  628.  
  629.         /// <summary>
  630.         /// Returns the ADGroupPrincipal object that matches the specified attribute value and match type
  631.         /// </summary>
  632.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  633.         /// <param name="attribute">The attribute name to be queried.</param>
  634.         /// <param name="value">The attribute value to match.</param>
  635.         /// <param name="mt">A System.DirectoryServices.AccountManagement.MatchType object that specifies the type of comparison.</param>
  636.         /// <returns>An ADGroupPrincipal object that matches the specified attribute value or null if no match is found.</returns>
  637.         public static ADGroupPrincipal FindOneByAttribute(PrincipalContext context, string attribute, string value)
  638.         {
  639.             return FindOneByAttribute(context, attribute, value, MatchType.Equals);
  640.         }
  641.  
  642.         /// <summary>
  643.         /// Returns the ADGroupPrincipal object that matches the specified attribute value and match type
  644.         /// </summary>
  645.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  646.         /// <param name="attribute">The attribute name to be queried.</param>
  647.         /// <param name="value">The attribute value to match.</param>
  648.         /// <param name="mt">A System.DirectoryServices.AccountManagement.MatchType object that specifies the type of comparison.</param>
  649.         /// <returns>An ADGroupPrincipal object that matches the specified attribute value or null if no match is found.</returns>
  650.         public static ADGroupPrincipal FindOneByAttribute(PrincipalContext context, string attribute, string value, MatchType mt)
  651.         {
  652.             ADGroupPrincipal filter = new ADGroupPrincipal(context);
  653.             filter.AdvancedSearchFilter.FindByAttribute(attribute, value, typeof(string), mt);
  654.             PrincipalSearcher ps = new PrincipalSearcher(filter);
  655.             return (ADGroupPrincipal)ps.FindOne();
  656.         }
  657.  
  658.         /// <summary>
  659.         /// Returns a List of ADGroupPrincipal objects that matches the values specified in the principal object
  660.         /// </summary>
  661.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  662.         /// <param name="ADUserPrincipal">The ADGroupPrincipal object to be matched from.</param>
  663.         /// <returns>A List of type ADGroupPrincipal containing the objects that match the specified principal values.</returns>
  664.         public static List<ADGroupPrincipal> FindByPrincipalSearcher(PrincipalContext principalContext, ADGroupPrincipal principal)
  665.         {
  666.             var results = new List<ADGroupPrincipal>();
  667.  
  668.             PrincipalSearcher ps = new PrincipalSearcher(principal);
  669.             foreach (ADGroupPrincipal p in ps.FindAll())
  670.             {
  671.                 results.Add(p);
  672.             }
  673.             return results;
  674.         }
  675.  
  676.         /// <summary>
  677.         /// Returns the ADGroupPrincipal objects that matches the values specified in the principal object
  678.         /// </summary>
  679.         /// <param name="principalContext">The System.DirectoryServices.AccountManagement.PrincipalContext that specifies the server or domain against which operations are performed.</param>
  680.         /// <param name="ADUserPrincipal">The ADGroupPrincipal object to be matched from.</param>
  681.         /// <returns>An ADGroupPrincipal objects that match the specified principal values or null if no match is found.</returns>
  682.         public static ADUserPrincipal FindOneByPrincipalSearcher(PrincipalContext principalContext, ADGroupPrincipal principal)
  683.         {
  684.             PrincipalSearcher ps = new PrincipalSearcher(principal);
  685.             return (ADUserPrincipal)ps.FindOne();
  686.         }
  687.  
  688.         /// <summary>
  689.         /// Adds a Principal to a group
  690.         /// </summary>
  691.         /// <param name="principal">The Principal to add to the group</param>
  692.         /// <param name="targetGroup">The ADGroupPrincipal that the Principal will be added to</param>
  693.         /// <param name="retryAttempts">Number of retry attempts</param>
  694.         /// <param name="retryDelay">Number of seconds to wait between retries</param>
  695.         /// <returns>The number of attempts taken to add the principal to the group, or -1 if unsuccessful</returns>
  696.         public int AddPrincipalToGroup(Principal principal, int retryAttempts = 0, int retryDelay = 0)
  697.         {
  698.             for (int x = 1; x <= retryAttempts; x++)
  699.             {
  700.                 try
  701.                 {
  702.                     int i = this.Members.Where(g => g.SamAccountName == principal.SamAccountName).Count();
  703.                     if (i == 0)
  704.                     {
  705.                         this.Members.Add(principal);
  706.                         this.Save();
  707.                     }
  708.                     return x;
  709.                 }
  710.                 catch
  711.                 {
  712.                     for (int x2 = 0; x2 < 10; x2++)
  713.                     {
  714.                         System.Threading.Thread.Sleep(100 * retryDelay);
  715.                     }
  716.                 }
  717.             }
  718.             return -1;
  719.         }
  720.     }
  721.  
  722.  
  723.     public class ADGroupPrincipalSearchFilter : AdvancedFilters
  724.     {
  725.         public ADGroupPrincipalSearchFilter(Principal p) : base(p) { }
  726.         public void ExtensionAttribute10(string value, MatchType mt)
  727.         {
  728.             this.AdvancedFilterSet("extensionAttribute10", value, typeof(string), mt);
  729.         }
  730.         public void FindByAttribute(string attribute, string value, Type objectType, MatchType mt)
  731.         {
  732.             this.AdvancedFilterSet(attribute, value, objectType, mt);
  733.         }
  734.     }
  735. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement