private void ResetAmbientUserAreaToDefault()
        {
            var defaultUserArea = _userAreaDefinitionRepository.GetDefault();

            EntityNotFoundException.ThrowIfNull(defaultUserArea, "Default");

            _ambientUserAreaCode = defaultUserArea.UserAreaCode;
        }
        /// <summary>
        /// Returns the authenitcation scheme name that should be set as the default
        /// in a call to <see cref="AuthenticationServiceCollectionExtensions.AddAuthentication"/>.
        /// Override this if you want to change the default scheme.
        /// </summary>
        protected virtual string GetDefaultScheme()
        {
            var defaultSchemeCode = _userAreaDefinitionRepository.GetDefault()?.UserAreaCode;

            if (defaultSchemeCode == null)
            {
                throw new InvalidOperationException("Default user area expected, but none are defined. The Cofoundry Admin user area should be defined at least.");
            }

            var defaultScheme = AuthenticationSchemeNames.UserArea(defaultSchemeCode);

            return(defaultScheme);
        }
        /// <summary>
        /// Creates a minimal service provider to support authentication
        /// </summary>
        private ServiceProvider CreateServiceProvider(IUserAreaDefinitionRepository userAreaDefinitionRepository)
        {
            var allUserAreas = userAreaDefinitionRepository.GetAll();

            var defaultSchemaCode = userAreaDefinitionRepository.GetDefault().UserAreaCode;
            var defaultScheme     = AuthenticationSchemeNames.UserArea(defaultSchemaCode);

            var services    = new ServiceCollection();
            var authBuilder = services.AddAuthentication(defaultScheme);

            foreach (var userAreaDefinition in allUserAreas)
            {
                var scheme = AuthenticationSchemeNames.UserArea(userAreaDefinition.UserAreaCode);
                authBuilder.AddCookie(scheme);
            }

            services.AddLogging(config => config.AddDebug().AddConsole());
            services.AddControllersWithViews();

            return(services.BuildServiceProvider());
        }