Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This solution is really slow, and when for example when you are using this in a webapplication GetAuthorizationGroups gets called very often which makes the site very slow. I worked implemented som caching instead, that makes alot faster after the first time. I am also retrying, because the exception still occurs.</p> <p>First i override the GetRolesForUser method and implement the caching.</p> <pre><code> public override string[] GetRolesForUser(string username) { // List of Windows groups for the given user. string[] roles; // Create a key for the requested user. string cacheKey = username + ":" + ApplicationName; // Get the cache for the current HTTP request. Cache cache = HttpContext.Current.Cache; // Attempt to fetch the list of roles from the cache. roles = cache[cacheKey] as string[]; // If the list is not in the cache we will need to request it. if (null == roles) { // Allow the base implementation to load the list of roles. roles = GetRolesFromActiveDirectory(username); // Add the resulting list to the cache. cache.Insert(cacheKey, roles, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration); } // Return the resulting list of roles. return roles; } </code></pre> <p>The GetRolesFromActiveDirectory looks like this.</p> <pre><code> public String[] GetRolesFromActiveDirectory(String username) { // If SQL Caching is enabled, try to pull a cached value.);)); if (_EnableSqlCache) { String CachedValue; CachedValue = GetCacheItem('U', username); if (CachedValue != "*NotCached") { return CachedValue.Split(','); } } ArrayList results = new ArrayList(); using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, _DomainDN)) { try { UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username); var tries = 0; var groups = GetAuthorizationGroups(p, tries); foreach (GroupPrincipal group in groups) { if (!_GroupsToIgnore.Contains(group.SamAccountName)) { if (_IsAdditiveGroupMode) { if (_GroupsToUse.Contains(group.SamAccountName)) { results.Add(group.SamAccountName); } } else { results.Add(group.SamAccountName); } } } } catch (Exception ex) { throw new ProviderException("Unable to query Active Directory.", ex); } } // If SQL Caching is enabled, send value to cache if (_EnableSqlCache) { SetCacheItem('U', username, ArrayListToCSString(results)); } return results.ToArray(typeof(String)) as String[]; } </code></pre> <p>The last method is GetAuthorizationGroups and it looks like this.</p> <pre><code> private PrincipalSearchResult&lt;Principal&gt; GetAuthorizationGroups(UserPrincipal userPrincipal, int tries) { try { return userPrincipal.GetAuthorizationGroups(); } catch(FileNotFoundException ex) { if (tries &gt; 5) throw; tries++; Thread.Sleep(1000); return GetAuthorizationGroups(userPrincipal, tries); } catch (AppDomainUnloadedException ex) { if (tries &gt; 5) throw; tries++; Thread.Sleep(1000); return GetAuthorizationGroups(userPrincipal, tries); } } </code></pre> <p>I found out that caching the roles makes it a whole lot faster. Hope this helps someone. Cheers.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload