Operate the ActiveDirectory object with Active Record pattern, forget hundreds of the AD attribute names and AD attribute const values, forget all the details of the ActiveDirectory and enjoy your self.

And you can also use the code to generate the filter to get the AD object search result, all the code is clear and simple.
More over: With the using code block you will dispose the no management resource easily. Now let's do it with the following guideline:

How to init the AD operator?

Just implement the "IADOperator" interface and then you can DI (dependency injection) the interface to the AD Object model. The following example is the mock implement with Moq framework in Unit Test.
protected IADOperator ADOperator { get; set; }

[TestFixtureSetUp]
public void FixtureSetUp()
{
     var mock = new Mock<IADOperator>();
     var adOperatorInfo = new ADOperatorInfo
              {
                      UserLoginName = TF.GetConfig().Properties["DomainUserName"],
                      Password = TF.GetConfig().Properties["DomainUserPassword"],
                      OperateDomainName = TF.GetConfig().Properties["DomainName"],
                };
      mock.Setup(m => m.GetOperatorInfo()).Returns(adOperatorInfo);
      this.ADOperator = mock.Object;
      this.SetUp();
}

This Library contains a build-in IADOperator interface implementation which is ADOperator class as following:
IADOperator adOperator = new ADOperator(@"landpy\pangxiaoliang", "password", "landpy");

You can implement your custom class to inherit IADOperator (Get the ADOperatorInfo by Mock, from DB, from file system and so on), and then use it as a param with the Lib Model

How to get all the user AD objects?

var userObjects = UserObject.FindAll(this.ADOperator);
foreach (UserObject userObject in userObjects)
{
       using (userObject)
       {
             // do something.
       }
}

Note: You can get all other type AD objects as above. (Such as Computer, Contact, Container, Group, Organizational Unit and so on)

How to get all the user AD objects with the special filter?

var userObjects = UserObject.FindAll(this.ADOperator, new StartWith(AttributeNames.CN, "pangxiaoliang"));
foreach (UserObject userObject in userObjects)
{
     using (userObject)
     {
          // do something.
     }
}

Note: You can get all other type AD objects as above. (Such as Computer, Contact, Container, Group, Organizational Unit and so on)

How to get one user AD object with sid?

using (var userObject = UserObject.FindOneBySid(this.ADOperator, “S-1-5-21-3069975210-3933090721-4243971453-1197”))
{
     // do something.
}

How to get one user AD object with sAMAccountName?

using (var userObject = UserObject.FindOneBySAMAccountName(this.ADOperator, "pangxiaoliang"))
{
      // do something.
}

How to get one user AD object with common name?

using (var userObject = UserObject.FindOneByCN(this.ADOperator, “pangxiaoliang”))
{
     // do something.
}

Note: You can get one other type AD object as above. (Such as Computer, Contact, Container, Group and so on)

How to update the user AD object?

using (var userObject = UserObject.FindOneByCN(this.ADOperator, “pangxiaoliang”))
{
     userObject.Email = "mv@live.cn";
     userObject.Save();
}

Note: You can update one other type AD object as above. (Such as Computer, Contact, Container, Group, Organizational Unit and so on)

How to delete the user AD object?

using (var userObject = UserObject.FindOneByCN(this.ADOperator, “pangxiaoliang”))
{
     userObject.Delete();
}

Note: You can delete one other type AD object as above. (Such as Computer, Contact, Container, Group, Organizational Unit and so on)

How to reset the user passwrod?

using (var userObject = UserObject.FindOneByCN(this.ADOperator, “pangxiaoliang”))
{
     userObject.ResetPassword("123!@#zxc");
}

How to get all the OU AD objects?

var organizationalUnitObjects = OrganizationalUnitObject.FindAll(this.ADOperator);
foreach (OrganizationalUnitObject organizationalUnitObject in organizationalUnitObjects)
{
    using (organizationalUnitObject)
    {
        // do something.
    }
}

How to get all the OU AD objects with special filter?

var organizationalUnitObjects = OrganizationalUnitObject.FindAll(this.ADOperator, new StartWith(OrganizationalUnitAttributeNames.OU, "pangxiaoliangou"));
foreach (OrganizationalUnitObject organizationalUnitObject in organizationalUnitObjects)
{
    using (organizationalUnitObject)
    {
        // do something.
    }
}

How to get one OU AD object with ou name?

using (var organizationalUnitObject = OrganizationalUnitObject.FindOneByOU(this.ADOperator, "pangxiaoliangou"))
{
    // do something.
}

How to add a new OU AD object?

using (var organizationalUnitObject = OrganizationalUnitObject.FindOneByOU(this.ADOperator, "pangxiaoliangou"))
{
    using (var addOrganizationalUnitObject = organizationalUnitObject.AddOrganizationalUnit("LandpyDemoOU"))
    {
        // do something.
    }
}

How to add a new Group AD object?

using (var organizationalUnitObject = OrganizationalUnitObject.FindOneByOU(this.ADOperator, "pangxiaoliangou"))
{
    using (var addGroupObject = organizationalUnitObject.AddGroup("LandpyDemoGroup"))
    {
        // do something.
    }
}

How to add a new User AD object?

using (var organizationalUnitObject = OrganizationalUnitObject.FindOneByOU(this.ADOperator, "pangxiaoliangou"))
{
    using (var addUserObject = organizationalUnitObject.AddUser("LandpyDemoUser"))
    {
        // do something.
    }
}

How to get all the group AD objects?

var groupObjects = GroupObject.FindAll(this.ADOperator);
foreach (GroupObject groupObject in groupObjects)
{
      using (groupObject)
      {
             Console.WriteLine(groupObject.Path);
      }
}

How to get all the group AD objects with special filter?

var groupObjects = GroupObject.FindAll(this.ADOperator, new StartWith(AttributeNames.CN, "pangxiaolianggroup"))
foreach (GroupObject groupObject in groupObjects)
{
    using (groupObject)
    {
        Assert.AreEqual(this.GroupCn, groupObject.CN);
    }
}

How to get one group AD object with sid?

using (var groupObject = GroupObject.FindOneBySid(this.ADOperator, "S-1-5-21-3069975210-3933090721-4243971453-8817"))
{
      // do something.
}

How to get one group AD object with common name?

using (var groupObject = GroupObject.FindOneByCN(this.ADOperator, "pangxiaolianggroup"))
{
      // do something.
}

How to update the group AD object?

using (var groupObject = GroupObject.FindOneByCN(this.ADOperator, "pangxiaolianggroup"))
{
      groupObject.Email = "mv@live.cn";
      groupObject.Save();
}

How to delete the group AD object?

using (var groupObject = GroupObject.FindOneByCN(this.ADOperator, "pangxiaolianggroup"))
{
      groupObject.Delete();
}

How to get all the computer AD objects?

var computerObjects = ComputerObject.FindAll(this.ADOperator);
foreach (ComputerObject computerObject in computerObjects)
{
    using (computerObject)
    {
        // do something.
    }
}

How to get all the computer AD objects with special filter?

var computerObjects = ComputerObject.FindAll(this.ADOperator, new StartWith(AttributeNames.CN, "pangcomputer"))
foreach (ComputerObject computerObject in computerObjects)
{
    using (computerObject)
    {
        // do something.
    }
}

Hot to get one computer AD object with sid?

using (var computerObject = ComputerObject.FindOneBySid(this.ADOperator, "S-1-5-21-3069975210-3933090721-4243971453-8818"))
{
     // do something.
}

Hot to get one computer AD object with common name?

using (var computerObject = ComputerObject.FindOneByCN(this.ADOperator, "pangxiaoliangcomputer"))
{
     // do something.
}

How to update computer AD object?

using (var computerObject = ComputerObject.FindOneByCN(this.ADOperator, "pangxiaoliangcomputer"))
{
     computerObject.Description = "The computer of pangxiaoliang.";
     computerObject.Save();
}

How to delete computer AD object?

using (var computerObject = ComputerObject.FindOneByCN(this.ADOperator, "pangxiaoliangcomputer"))
{
     computerObject.Delete();
}

How to get AD object with object guid?

using (var adObject = ADObject.FindOneByObjectGUID(this.ADOperator, new Guid("ac8d3bca-4132-41b1-b29c-9dcfd860f50d")))
{
    var groupObject = adObject as GroupObject;
    if(groupObject != null)
    {
         // do something.
    }
}


How to get domain group policy minimum password length and whether need to meet the complexity requirments?

using (var domainObject = DomainObject.FindOne(this.ADOperator))
{
    Console.WriteLine(this.DomainGroupPolicyMinimumPasswordLength);
    Console.WriteLine(this.DomianIsMustMeetComplexityRequirments);
}

How to get the current domain object which the server is join in without password?

using (var domainObject = DomainObject.GetCurrent())
{
       Console.WriteLine(domainObject.Name);
}

How to get the special user's domain PSO custom policy minimum password length and whether need to meet the complexity requirements?

foreach (var passwordSettingsObject in PasswordSettingsObject.FindAll(this.ADOperator, "pangxiaoliang"))
{
    using (passwordSettingsObject)
    {
        Console.WriteLine(passwordSettingsObject.CustomPolicyMinimumPasswordLength);
        Console.WriteLine(passwordSettingsObject.IsMustMeetComplexityRequirments);
    }
}

How to verify the password is valid?

this.PasswordUnity.IsPasswordValid(@"landpy\pangxiaoliang", @"123!@#qwe")

How to verify the AD object exists?

ADObject.DoesADObjectExists(this.ADOperator, new Guid("ac8d3bca-4132-41b1-b29c-9dcfd860f50d"))

How to query AD object with ObjectGuid attribute filter? (Already resolve Guid endian issue)

// Normal filter
var adObject = ADObjectQuery.SingleAndDefault(this.ADOperator, new Is(AttributeNames.ObjectGuid, "ac8d3bca-4132-41b1-b29c-9dcfd860f50d"));
// Custom filter
adObject = ADObjectQuery.SingleAndDefault(this.ADOperator, new Custom(String.Format(@"{0}={1}", AttributeNames.ObjectGuid, "ac8d3bca-4132-41b1-b29c-9dcfd860f50d")));

How to search AD objects (You can use the filters which are list at section "Support filter type are as following" to composite query to search the result what you want)?

// Query user
foreach (UserObject user in ADObjectQuery.List(this.ADOperator, new IsUser()))
{
    using (user)
    {
        // do something.
    }
}
// Query Person
foreach (PersonObject person in ADObjectQuery.List(this.ADOperator, new IsPerson()))
{
    using (person)
    {
        // do someting.
    }
}
// Query Contact
foreach (ContactObject contact in ADObjectQuery.List(this.ADOperator, new IsContact()))
{
    using (contact)
    {
        // do something.
    }
}
// Query ADObject (Will return UserObject and ContactObject which email address contains "live")
foreach (var adObject in ADObjectQuery.List(this.ADOperator, new Contains(PersonAttributeNames.Mail, "live")))
{
    using (adObject)
    {
        if(adObject.Type==ADObjectType.User)
        {
               var user = adObject as User;
               // do something.
        }
        if(adObject.Type==ADObjectType.Contact)
        {
               var contact= adObject as Contact;
               // do something.
        }
    }
}

Support filter type are as following:

  1. And
  2. Or
  3. StartWith
  4. EndWith
  5. HasAValue
  6. HasNoValue
  7. Is
  8. IsNot
  9. IsPerson
  10. IsUser
  11. IsGroup
  12. IsContact
  13. IsComputer
  14. IsContainer
  15. IsOU
  16. IsDomain
  17. IsPasswordSettings
  18. Contains
  19. LessThanOrEqualTo
  20. GreaterThanOrEqualTo
  21. Approx
  22. Custom (Use native AD filter string)

Support AD object type are as following:

  1. UserObject
  2. GroupObject
  3. ContactObject
  4. OrganizationalUnitObject
  5. ComputerObject
  6. ContainerObject
  7. DomainObject
  8. InetOrgPersonObject
  9. MSMQQueueAliasObject
  10. PasswordSettingsObject
  11. PrinterObject
  12. SharedFolderObject

Support AD object attribute property are as following:

  1. LDAP Path
  2. CN
  3. ObjectGuid
  4. DistinguishedName
  5. Name
  6. CanonicalName
  7. CreateTime
  8. ModifyTime
  9. Description
  10. DirectReports
  11. DisplayName
  12. MsDS_PrincipalName
  13. Office
  14. ZipOrPostalCode
  15. PostOfficeBoxs
  16. WebPage
  17. OtherWebPages
  18. ThumbnailPhoto
  19. ThumbnailLogo
  20. Email
  21. CO
  22. C
  23. Company
  24. CountryCode
  25. Department
  26. Fax
  27. OtherFaxes
  28. GivenName
  29. HomePhone
  30. OtherHomePhones
  31. Notes
  32. Initials
  33. IpPhone
  34. OtherIpPhones
  35. City
  36. Manager
  37. MemberOf
  38. Mobile
  39. OtherMobiles
  40. Pager
  41. OtherPagers
  42. Telephone
  43. OtherTelephones
  44. LastName
  45. StateOrProvince
  46. StreetAddress
  47. JobTitle
  48. ObjectSid
  49. SAMAccountName
  50. PrincipalName
  51. GroupSids
  52. AccountControlType
  53. IsDomainAdmin
  54. IsAccountOperator
  55. IsMustChangePwdNextLogon
  56. IsEnabled
  57. IsLocked
  58. OperatingSystemName
  59. OperatingSystemVersion
  60. OperatingSystemServicePack
  61. DnsName
  62. SiteName
  63. GroupPolicyMinimumPasswordLength
  64. IsMustMeetComplexityRequirments
  65. GroupType
  66. GroupScope
  67. OU
  68. Street
  69. ManagedBy
  70. PSO CustomPolicyMinimumPasswordLength
  71. PSO IsMustMeetComplexityRequirments
  72. And so on


Note: For more information please see the Unit Test project source code of the solution, and the project contains detailed the summary comments you can generate the help doc with sandcastle yourself or download it in downloads tab! https://landpyactivedirectory.codeplex.com/releases/view/110605

There are some attributes or special AD object are not implement, but you can also expend them easily! And in the future the lib will add DSL feature to support search AD object results

Moreover:
For UserAttributeNames.AccountExpires attribute name 0 or 9223372036854775807 means 'never expires'.
If the AD attribute is nomal datetime you should use the filter as following:
new LessThanOrEqualTo(AttributeNames.CreateTimeStamp, "20141224010202.0Z")
Eg: dateTime.ToString("yyyyMMddHHmmss.0Z")
else if the AD attriubte is large integer datetime you should use the filer as following:
new LessThanOrEqualTo(UserAttributeNames.AccountExpires, "1305008640000000000")
Eg: dateTime.ToFileTimeUtc().ToString()

Convert DateTime to large integer:
accountExpiresDateTime.ToFileTimeUtc()
Convert large integer to DateTime:
long fileTime = 130314240000000000;
var dateTime = DateTime.FromFileTime(fileTime);

Last edited Jan 26, 2016 at 7:30 AM by pangxiaoliang, version 27

Comments

No comments yet.