/// <summary>
        /// Gets an existing Profile object from the cache.
        /// </summary>
        /// <param name="token">
        /// Token identifying an existing cached entity.
        /// </param>
        /// <returns>A cached Profile object</returns>
        public object GetProfile(IToken token)
        {
            SecurityCacheItem item    = GetSecurityCacheItem(token);
            object            profile = item == null ? null : item.Profile;

            SecurityCacheReadEvent.Fire(profile != null);

            return(profile);
        }
        /// <summary>
        /// Gets an existing IIdentity object from the cache.
        /// </summary>
        /// <param name="token">
        /// Token identifying an existing cached entity.
        /// </param>
        /// <returns>A cached IIdentity object</returns>
        public IIdentity GetIdentity(IToken token)
        {
            SecurityCacheItem item     = GetSecurityCacheItem(token);
            IIdentity         identity = item == null ? null : item.Identity;

            SecurityCacheReadEvent.Fire(identity != null);

            return(identity);
        }
        /// <summary>
        /// Gets an existing IPrincipal object from the cache.
        /// </summary>
        /// <param name="token">
        /// Token identifying an existing cached entity.
        /// </param>
        /// <returns>A cached IPrincipal object</returns>
        public IPrincipal GetPrincipal(IToken token)
        {
            SecurityCacheItem item      = GetSecurityCacheItem(token);
            IPrincipal        principal = item == null ? null : item.Principal;

            SecurityCacheReadEvent.Fire(principal != null);

            return(principal);
        }
        /// <summary>
        /// Gets an existing Profile object from the cache.
        /// </summary>
        /// <param name="token">
        /// Token identifying an existing cached entity.
        /// </param>
        /// <returns>A cached Profile object</returns>
        public override object GetProfile(IToken token)
        {
            SecurityCacheItem item    = GetSecurityCacheItem(token);
            object            profile = item == null ? null : item.Profile;

            InstrumentationProvider.FireSecurityCacheReadPerformed(SecurityEntityType.Profile, token);

            return(profile);
        }
        /// <summary>
        /// Gets an existing IPrincipal object from the cache.
        /// </summary>
        /// <param name="token">
        /// Token identifying an existing cached entity.
        /// </param>
        /// <returns>A cached IPrincipal object</returns>
        public override IPrincipal GetPrincipal(IToken token)
        {
            SecurityCacheItem item      = GetSecurityCacheItem(token);
            IPrincipal        principal = item == null ? null : item.Principal;

            InstrumentationProvider.FireSecurityCacheReadPerformed(SecurityEntityType.Principal, token);

            return(principal);
        }
        /// <summary>
        /// Gets an existing IIdentity object from the cache.
        /// </summary>
        /// <param name="token">
        /// Token identifying an existing cached entity.
        /// </param>
        /// <returns>A cached IIdentity object</returns>
        public override IIdentity GetIdentity(IToken token)
        {
            SecurityCacheItem item     = GetSecurityCacheItem(token);
            IIdentity         identity = item == null ? null : item.Identity;

            InstrumentationProvider.FireSecurityCacheReadPerformed(SecurityEntityType.Identity, token);

            return(identity);
        }
        private SecurityCacheItem GetSecurityCacheItem(IToken token, bool createIfNull)
        {
            SecurityCacheItem item = null;

            item = securityCacheManager.GetData(token.Value) as SecurityCacheItem;

            if (item == null && createIfNull)
            {
                item = new SecurityCacheItem();
                securityCacheManager.Add(token.Value, item, CacheItemPriority.Normal, null, GetCacheExpirations());
            }

            return(item);
        }
        private void ExpireSecurityCacheItem(IToken token, SecurityEntityType entityType)
        {
            SecurityCacheItem item = GetSecurityCacheItem(token);

            if (item != null)
            {
                ClearCachItemEntity(item, entityType);

                if (item.IsRemoveable)
                {
                    securityCacheManager.Remove(token.Value);
                }
            }
        }
        private void ClearCachItemEntity(SecurityCacheItem item, SecurityEntityType entityType)
        {
            switch (entityType)
            {
            case SecurityEntityType.Identity:
                item.Identity = null;
                break;

            case SecurityEntityType.Principal:
                item.Principal = null;
                break;

            case SecurityEntityType.Profile:
                item.Profile = null;
                break;
            }
        }
		private SecurityCacheItem GetSecurityCacheItem(IToken token, bool createIfNull)
		{
			SecurityCacheItem item = null;
			item = securityCacheManager.GetData(token.Value) as SecurityCacheItem;

			if (item == null && createIfNull)
			{
				item = new SecurityCacheItem();
				securityCacheManager.Add(token.Value, item, CacheItemPriority.Normal, null, GetCacheExpirations());
			}

			return item;
		}
		private void ClearCachItemEntity(SecurityCacheItem item, SecurityEntityType entityType)
		{
			switch (entityType)
			{
				case SecurityEntityType.Identity:
					item.Identity = null;
					break;
				case SecurityEntityType.Principal:
					item.Principal = null;
					break;
				case SecurityEntityType.Profile:
					item.Profile = null;
					break;
			}
		}