kamagra how much to take

PrincipalContext Objects And Performance

Using the new namespaces for AD stuff is really nice, but MSFT really dropped the ball when building PrincipalContext objects. PrincipalContext objects are used to encapsulate the server or domain which are going to be subject to the AD operations, so is hydrated when building UserPrincipal or GroupPrincipal objects. So, for example ,they are generally put into static methods such as:

  1. public static PrincipalContext GetGroupPrincipalContext()
  2. {
  3. PrincipalContext principalContext = null;
  4. SPSecurity.RunWithElevatedPrivileges(() => principalContext = new PrincipalContext(ContextType.Domain, "<domain controller>", "<some OU path>"));
  5. return principalContext;
  6. }

so that we can use it later for user and group operations:

  1. public static UserPrincipal GetUser(string userName)
  2. {
  3. UserPrincipal userPrincipal = null;
  4. SPSecurity.RunWithElevatedPrivileges(() =>
  5. {
  6. PrincipalContext principalContext = GetUserPrincipalContext();
  7. userPrincipal = UserPrincipal.FindByIdentity(principalContext, userName);
  8. });
  9. return userPrincipal;
  10. }
  12. public static GroupPrincipal GetGroup(string groupName)
  13. {
  14. GroupPrincipal groupPrincipal = null;
  15. SPSecurity.RunWithElevatedPrivileges(() =>
  16. {
  17. PrincipalContext principalContext = GetGroupPrincipalContext();
  18. groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
  19. });
  20. return groupPrincipal;
  21. }

However, it is important that if you experience performance issues to consider two things. I have noticed that with declarative domain controller specific rather than relying on the round-robin default fashion is pretty effective. Otherwise, you are going to be limited to using Attribute Scope Query (ASQ). This involves using theĀ DirectoryEntry and DirectorySearcher objects.


An update to this post is available here.


No Comments


  1. System.DirectoryServices.AccountManagement, PrincipalContext, Principals, And Performance | ARB Security Solutions - [...] Posts System.DirectoryServices.AccountManagement, PrincipalContext, Principals, And Performance PrincipalContext Objects And PerformanceRemove Duplicates From Strongly Typed Collections With Extension MethodsEnumerating …

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>