Advertisement
EmilySamantha80

Full featured Active Directory client in C# ADUser.cs

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