private bool TryAddContext(SecurityContextSecurityToken token, bool throwOnFailure)
        {
            if (token == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token");
            }
            if (!System.ServiceModel.Security.SecurityUtils.IsCurrentlyTimeEffective(token.ValidFrom, token.ValidTo, this.clockSkew))
            {
                if (token.KeyGeneration == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("SecurityContextExpiredNoKeyGeneration", new object[] { token.ContextId }));
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("SecurityContextExpired", new object[] { token.ContextId, token.KeyGeneration.ToString() }));
            }
            if (!System.ServiceModel.Security.SecurityUtils.IsCurrentlyTimeEffective(token.KeyEffectiveTime, token.KeyExpirationTime, this.clockSkew))
            {
                if (token.KeyGeneration == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("SecurityContextKeyExpiredNoKeyGeneration", new object[] { token.ContextId }));
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("SecurityContextKeyExpired", new object[] { token.ContextId, token.KeyGeneration.ToString() }));
            }
            object hashKey = this.GetHashKey(token.ContextId, token.KeyGeneration);
            bool   flag    = base.TryAddItem(hashKey, token.Clone(), false);

            if (flag || !throwOnFailure)
            {
                return(flag);
            }
            if (token.KeyGeneration == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("ContextAlreadyRegisteredNoKeyGeneration", new object[] { token.ContextId })));
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("ContextAlreadyRegistered", new object[] { token.ContextId, token.KeyGeneration.ToString() })));
        }
 public SecurityContextSecurityToken GetContext(UniqueId contextId, UniqueId generation)
 {
     if (contextId == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId));
     }
     object hashKey = GetHashKey(contextId, generation);
     SecurityContextSecurityToken sct = (SecurityContextSecurityToken)GetItem(hashKey);
     return sct != null ? (SecurityContextSecurityToken)sct.Clone() : null;
 }
        private bool TryAddContext(SecurityContextSecurityToken token, bool throwOnFailure)
        {
            if (token == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token));
            }

            if (!SecurityUtils.IsCurrentlyTimeEffective(token.ValidFrom, token.ValidTo, _clockSkew))
            {
                if (token.KeyGeneration == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityContextExpiredNoKeyGeneration, token.ContextId));
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityContextExpired, token.ContextId, token.KeyGeneration.ToString()));
                }
            }

            if (!SecurityUtils.IsCurrentlyTimeEffective(token.KeyEffectiveTime, token.KeyExpirationTime, _clockSkew))
            {
                if (token.KeyGeneration == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityContextKeyExpiredNoKeyGeneration, token.ContextId));
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityContextKeyExpired, token.ContextId, token.KeyGeneration.ToString()));
                }
            }

            object hashKey = GetHashKey(token.ContextId, token.KeyGeneration);
            bool wasTokenAdded = TryAddItem(hashKey, (SecurityContextSecurityToken)token.Clone(), false);
            if (!wasTokenAdded)
            {
                if (throwOnFailure)
                {
                    if (token.KeyGeneration == null)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ContextAlreadyRegisteredNoKeyGeneration, token.ContextId)));
                    }
                    else
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ContextAlreadyRegistered, token.ContextId, token.KeyGeneration.ToString())));
                    }
                }
            }
            return wasTokenAdded;
        }
        public SecurityContextSecurityToken GetContext(UniqueId contextId, UniqueId generation)
        {
            if (contextId == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contextId");
            }
            object hashKey = this.GetHashKey(contextId, generation);
            SecurityContextSecurityToken item = (SecurityContextSecurityToken)base.GetItem(hashKey);

            if (item == null)
            {
                return(null);
            }
            return(item.Clone());
        }