コード例 #1
0
        private bool TryAddContext(SecurityContextSecurityToken token, bool throwOnFailure)
        {
            if (token == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token));
            }

            if (!SecurityUtils.IsCurrentlyTimeEffective(token.ValidFrom, token.ValidTo, this.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, this.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 = base.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);
        }