Exemplo n.º 1
0
        private static bool TryGetCachedPrincipalData(Guid guid, out ADCachedPrincipal cacheData)
        {
            TimeSpan cacheExpiry =
                ConfigurationHelper.ParseTimeSpanOrDefault(
                    ConfigurationManager.AppSettings["ActiveDirectoryPrincipalCacheExpiry"],
                    s_defaultPrincipalCacheExpiry
                    );

            for (int i = s_cachedPrincipals.Count - 1; i >= 0; i--)
            {
                ADCachedPrincipal dataSet = s_cachedPrincipals[i];

                if (DateTime.UtcNow.Subtract(cacheExpiry) > dataSet.CacheTime)
                {
                    Log.Verbose("ADCACHE: Cache expired for {Guid}", guid);

                    dataSet.Dispose();
                    s_cachedPrincipals.RemoveAt(i);
                    continue;
                }

                if (dataSet.Principal.Guid == guid)
                {
                    Log.Verbose("ADCACHE: Cache hit for {Guid}", guid);

                    cacheData = dataSet;
                    return(true);
                }
            }

            Log.Verbose("ADCACHE: Cache miss for {Guid}", guid);

            cacheData = null;
            return(false);
        }
Exemplo n.º 2
0
        private static bool TryCacheUserPrincipalData(Domain domain, Guid id, out ADCachedPrincipal cacheData)
        {
            cacheData = null;

            try
            {
                PrincipalContext pc = GetPrincipalContext(ContextType.Domain, domain.Name);

                Log.Information("Looking for user with guid {guid} in domain {domain}", id.ToString(), domain.Name);

                var user = UserPrincipal.FindByIdentity(pc, IdentityType.Guid, id.ToString());
                if (user != null)
                {
                    cacheData = new ADCachedPrincipal(pc, user);
                    s_cachedPrincipals.Add(cacheData);
                    return(true);
                }
            }
            catch (Exception exp)
            {
                Log.Error(exp, "AD: Failed to find user with guid {GUID}", id.ToString());
                // let it fail
            }

            return(false);
        }
Exemplo n.º 3
0
        private static bool TryCacheGroupPrincipalData(Domain domain, string name, out ADCachedPrincipal cacheData)
        {
            cacheData = null;

            try
            {
                var pc = new PrincipalContext(ContextType.Domain, domain.Name);

                Log.Information("Searching for group {name} in domain {domain}", name, domain.Name);

                var group = GroupPrincipal.FindByIdentity(pc, IdentityType.Name, name);

                if (group != null)
                {
                    cacheData = new ADCachedPrincipal(pc, group);
                    s_cachedPrincipals.Add(cacheData);
                    return(true);
                }
            }
            catch (Exception exp)
            {
                Log.Error(exp, "GetPrincipal Group with name: " + name);
                // let it fail
            }

            return(false);
        }
Exemplo n.º 4
0
        private static bool TryGetCachedPrincipalData(string name, out ADCachedPrincipal cacheData)
        {
            bool     searchUpn   = name.Contains("@");
            TimeSpan cacheExpiry =
                ConfigurationHelper.ParseTimeSpanOrDefault(
                    ConfigurationManager.AppSettings["ActiveDirectoryPrincipalCacheExpiry"],
                    s_defaultPrincipalCacheExpiry
                    );

            for (int i = s_cachedPrincipals.Count - 1; i >= 0; i--)
            {
                ADCachedPrincipal dataSet = s_cachedPrincipals[i];

                if (DateTime.UtcNow.Subtract(cacheExpiry) > dataSet.CacheTime)
                {
                    Log.Verbose("ADCACHE: Cache expired for {Name}", name);

                    dataSet.Dispose();
                    s_cachedPrincipals.RemoveAt(i);
                    continue;
                }

                if (searchUpn && !string.IsNullOrEmpty(dataSet.Principal.UserPrincipalName))
                {
                    if (dataSet.Principal.UserPrincipalName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        Log.Verbose("ADCACHE: Cache hit for {UPN}", name);

                        cacheData = dataSet;
                        return(true);
                    }
                }
                else
                {
                    if (dataSet.Principal.SamAccountName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        Log.Verbose("ADCACHE: Cache hit for {SamAccountName}", name);

                        cacheData = dataSet;
                        return(true);
                    }
                }
            }

            Log.Verbose("ADCACHE: Cache miss for {Name}", name);

            cacheData = null;
            return(false);
        }
Exemplo n.º 5
0
        private static bool TryCacheUserPrincipalData(Domain domain, string fullUsername, string strippedUsername, out ADCachedPrincipal cacheData)
        {
            cacheData = null;

            try
            {
                PrincipalContext pc = GetPrincipalContext(ContextType.Domain, domain.Name);

                UserPrincipal principalBySamName = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, strippedUsername);
                if (principalBySamName != null)
                {
                    cacheData = new ADCachedPrincipal(pc, principalBySamName);
                    s_cachedPrincipals.Add(cacheData);
                    return(true);
                }

                Log.Verbose("TryCachePrincipalData: Did not find user {UserName} in domain {DomainName} by SamAccountName", strippedUsername, domain.Name);

                UserPrincipal principalByUPN = UserPrincipal.FindByIdentity(pc, IdentityType.UserPrincipalName, fullUsername);
                if (principalByUPN != null)
                {
                    cacheData = new ADCachedPrincipal(pc, principalByUPN);
                    s_cachedPrincipals.Add(cacheData);
                    return(true);
                }

                Log.Verbose(
                    "TryCachePrincipalData: Did not find user {UserName} in domain {DomainName} by UPN",
                    fullUsername, domain.Name);
            }
            catch (Exception exp)
            {
                Log.Error(exp, "TryCachePrincipalData in domain: {DomainName}, user: {FullUserName} ({StrippedUserName})", domain.Name, fullUsername, strippedUsername);
            }

            return(false);
        }