Azure功能未注册身份验证处理程序

我正在尝试创建一个简单的V2 Azure函数。它可以是匿名身份验证级别,因为它将由API Management保护。

构造器和函数如下所示:

        public ProvideOfsPortfolioData(IMediator mediatorService,IInstrumentationService instrumentationService,ILogger<ProvideOfsPortfolioData> logger)
        {
            _mediatorService = mediatorService;
            _instrumentationService = instrumentationService;
            _logger = logger;
            _typeName = "Interfaces.Ce.Presentation.AzureFunctions.ProvideOfsPortfolioData";
        }

        [FunctionName("ProvideOfsPortfolioData")]
        public async Task<IactionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous,"get",Route = null)] HttpRequest req)
        {
            _logger.LogInformation($"{_typeName}.Run] - Started");
        }

当我尝试通过在VS2019中按F5进行调试时,在控制台窗口中出现以下错误:

An unhandled host error has occurred.
microsoft.AspNetCore.Authentication.Core: No authentication handlers
are registered. Did you forget to call
AddAuthentication().Add[SomeAuthHandler]("WebJobsAuthLevel",...)?.

Startup.cs的内容如下:

[assembly: WebJobsStartup(typeof(Startup))]
namespace Interfaces.Ce.Presentation.AzureFunctions
{
    /// <summary>
    /// Startup.
    /// </summary>
    public class Startup : IWebJobsStartup
    {
        private Dictionary<string,string> _metadataTokens;

        // Public Methods.
        #region PublicMethods

        /// <summary>
        /// Configure.
        /// </summary>
        /// <param name="builder"></param>
        public void Configure(IWebJobsBuilder builder)
        {
            // Get Service Configuration.
            ReadServiceConfiguration();

            // Add framework services.
            builder.Services.AddLogging();
            builder.Services.AddLookupService(_metadataTokens);
            builder.Services.AddCrmService(_metadataTokens);
            builder.Services.AddInstrumentationService(_metadataTokens);

            builder.Services.AddTransient(typeof(IMandateService),Type.GetType(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.MandateService)));
            builder.Services.AddTransient(typeof(IPolicyService),Type.GetType(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.PolicyService)));
            builder.Services.AddTransient(typeof(IPortfolioService),Type.GetType(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.PortfolioService)));
            builder.Services.AddTransient(typeof(IReviewService),Type.GetType(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.ReviewService)));
            builder.Services.AddTransient(typeof(IContactService),Type.GetType(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.ContactService)));
            builder.Services.AddTransient(typeof(IInvestmentProfileService),Type.GetType(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.InvestmentProfileService)));
            builder.Services.AddTransient(typeof(ISystemUserService),Type.GetType(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.SystemUserService)));
            builder.Services.AddTransient(typeof(ITeamService),Type.GetType(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.TeamService)));

            // =======================
            // MediatR Initialisation.
            // =======================
            builder.Services.AddScoped(typeof(IMediator),typeof(Mediator));
            builder.Services.AddTransient<ServiceFactory>(p => p.GetService);
            builder.Services.AddTransient(typeof(IPipelineBehavior<,>),typeof(RequestPreProcessorBehavior<,>));
            builder.Services.AddTransient(typeof(IPipelineBehavior<,typeof(RequestPostProcessorBehavior<,>));

            // ============================
            // MediatR Pipeline Extensions.
            // ============================
            if (Convert.ToBoolean(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.PipelineProcessingRequestValidationEnabled)))
            {
                builder.Services.AddMvc().AddFluentValidation(fv =>
                    fv.RegisterValidatorsFromAssemblyContaining<
                        Application.Portfolio.Commands.AssignSinglePortfolio.Validator>());
                builder.Services.AddTransient(typeof(IPipelineBehavior<,typeof(RequestValidationBehaviour<,>));
            }
            if (Convert.ToBoolean(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.PipelineProcessingPerformanceBehaviourEnabled)))
            {
                builder.Services.AddTransient(typeof(IPipelineBehavior<,typeof(RequestPerformanceBehaviour<,>));
            }

            // ========================================
            // MediatR - Commands/Queries Registration.
            // ========================================
            builder.Services.AddMediatR(typeof(Application.Portfolio.Commands.AssignSinglePortfolio.Request).GetTypeInfo().Assembly);


        }

        #endregion

        // Private Methods.
        #region PrivateMethods

        /// <summary>
        /// Read Configuration.
        /// </summary>
        private void ReadServiceConfiguration()
        {
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.getcurrentDirectory())
                .AddJsonFile("local.settings.json",true,true)
                .AddEnvironmentVariables()
                .Build();

            ValidateServiceConfiguration(config);
        }

        /// <summary>
        /// Validate Configuration.
        /// </summary>
        /// <param name="config"></param>
        private void ValidateServiceConfiguration(IConfigurationRoot config)
        {
            _metadataTokens = new Dictionary<string,string>();

            // Mandatory Service Configuration Keys.
            var listofkeys = new List<string>()
            {
                LocalSettings.ServiceConfigurationKeys.LookupService,LocalSettings.ServiceConfigurationKeys.InstrumentationService,LocalSettings.ServiceConfigurationKeys.PipelineProcessingRequestValidationEnabled,LocalSettings.ServiceConfigurationKeys.PipelineProcessingPerformanceBehaviourEnabled,LocalSettings.ServiceConfigurationKeys.MandateService,LocalSettings.ServiceConfigurationKeys.PolicyService,LocalSettings.ServiceConfigurationKeys.PortfolioService,LocalSettings.ServiceConfigurationKeys.ReviewService,LocalSettings.ServiceConfigurationKeys.ContactService,LocalSettings.ServiceConfigurationKeys.InvestmentProfileService,LocalSettings.ServiceConfigurationKeys.SystemUserService,LocalSettings.ServiceConfigurationKeys.TeamService
            };

            // Check for mandatory keys.
            foreach (var key in listofkeys)
            {
                if (string.IsnullOrEmpty(config[key]))
                {
                    throw new Exception($"the configuration key [{key}] is null or empty!");
                }
                _metadataTokens.Add(key,config[key]);
            }

            // Check for optional keys driven off condition flags.

            // Lookup Service Is Enabled.
            if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsEnabled]))
            {
                throw new Exception($"the configuration key [{LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsEnabled}] is null of empty!");
            }
            _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsEnabled,config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsEnabled]);

            // Crm Service Is Enabled.
            if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.CrmServiceApiIsEnabled]))
            {
                throw new Exception($"the configuration key [{LocalSettings.ServiceConfigurationKeys.CrmServiceApiIsEnabled}] is null of empty!");
            }
            _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.CrmServiceApiIsEnabled,config[LocalSettings.ServiceConfigurationKeys.CrmServiceApiIsEnabled]);

            // Authentication Service Is Enabled.
            if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceIsEnabled]))
            {
                throw new Exception($"the configuration key [{LocalSettings.ServiceConfigurationKeys.AuthenticationServiceIsEnabled}] is null of empty!");
            }
            _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.AuthenticationServiceIsEnabled,config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceIsEnabled]);

            // Instrumentation Service Is Enabled.
            if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiIsEnabled]))
            {
                throw new Exception($"the configuration key [{LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiIsEnabled}] is null of empty!");
            }
            _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiIsEnabled,config[LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiIsEnabled]);

            // If Enabled - Lookup Service Api Validation.
            if (Convert.ToBoolean(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsEnabled)))
            {
                // Mandatory.
                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiBaseAddress]))
                {
                    throw new Exception($"lookup service api key:[{LocalSettings.ServiceConfigurationKeys.LookupServiceApiBaseAddress}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.LookupServiceApiBaseAddress,config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiBaseAddress]);

                // Mandatory.
                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiSubscriptionKey])
                )
                {
                    throw new Exception($"lookup service api key:[{LocalSettings.ServiceConfigurationKeys.LookupServiceApiSubscriptionKey}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.LookupServiceApiSubscriptionKey,config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiSubscriptionKey]);


                // Mandatory.
                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsCachingEnabled]))
                {
                    throw new Exception($"lookup service api key:[{LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsCachingEnabled}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsCachingEnabled,config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiIsCachingEnabled]);


                // Optional.
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.LookupServiceApiCacheProviderServiceTypeName,string.IsnullOrEmpty(
                        config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiCacheProviderServiceTypeName])
                        ? string.Empty
                        : config[LocalSettings.ServiceConfigurationKeys.LookupServiceApiCacheProviderServiceTypeName]);
            }

            // If Enabled - Crm Service Api Validation.
            if (Convert.ToBoolean(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.CrmServiceApiIsEnabled)))
            {
                // Mandatory.
                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.CrmServiceApiBaseAddress]))
                {
                    throw new Exception($"crm service api key:[{LocalSettings.ServiceConfigurationKeys.CrmServiceApiBaseAddress}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.CrmServiceApiBaseAddress,config[LocalSettings.ServiceConfigurationKeys.CrmServiceApiBaseAddress]);

                // Mandatory.
                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.CrmServiceTimeout]))
                {
                    throw new Exception($"crm service timeout:[{LocalSettings.ServiceConfigurationKeys.CrmServiceTimeout}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.CrmServiceTimeout,config[LocalSettings.ServiceConfigurationKeys.CrmServiceTimeout]);

                if (!Convert.ToBoolean(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.AuthenticationServiceIsEnabled)))
                {
                    throw new Exception("Service dependency exception,Authentication Service must be enabled in conjunction with Crm Service. Check Configuration!");
                }
            }

            // If Enabled - Authentication Service APi Validation.
            if (Convert.ToBoolean(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.AuthenticationServiceIsEnabled)))
            {
                // Mandatory.
                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceAuthorityUrl]))
                {
                    throw new Exception(
                        $"authentication service key:[{LocalSettings.ServiceConfigurationKeys.AuthenticationServiceAuthorityUrl}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.AuthenticationServiceAuthorityUrl,config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceAuthorityUrl]);

                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceclientId]))
                {
                    throw new Exception(
                        $"authentication service key:[{LocalSettings.ServiceConfigurationKeys.AuthenticationServiceclientId}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.AuthenticationServiceclientId,config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceclientId]);

                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceRedirectUrl]))
                {
                    throw new Exception(
                        $"authentication service key:[{LocalSettings.ServiceConfigurationKeys.AuthenticationServiceRedirectUrl}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.AuthenticationServiceRedirectUrl,config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceRedirectUrl]);

                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceSecret]))
                {
                    throw new Exception(
                        $"authentication service key:[{LocalSettings.ServiceConfigurationKeys.AuthenticationServiceSecret}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.AuthenticationServiceSecret,config[LocalSettings.ServiceConfigurationKeys.AuthenticationServiceSecret]);
            }

            // If Enabled - Instrumentation Service APi Validation.
            if (Convert.ToBoolean(_metadataTokens.ReadString(LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiIsEnabled)))
            {
                // Mandatory.
                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiSubscriptionKey]))
                {
                    throw new Exception($"instrumentation data logger api subscription key:[{LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiSubscriptionKey}] is null or empty!");
                }
                _metadataTokens.Add(
                    LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiSubscriptionKey,config[LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiSubscriptionKey]);

                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiBaseAddress]))
                {
                    throw new Exception($"instrumentation data logger service api base address key:[{LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiBaseAddress}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiBaseAddress,config[LocalSettings.ServiceConfigurationKeys.InstrumentationServiceApiBaseAddress]);

                if (string.IsnullOrEmpty(config[LocalSettings.ServiceConfigurationKeys.InstrumentationServiceMessageaction]))
                {
                    throw new Exception($"instrumentation data logger service message action key:[{LocalSettings.ServiceConfigurationKeys.InstrumentationServiceMessageaction}] is null or empty!");
                }
                _metadataTokens.Add(LocalSettings.ServiceConfigurationKeys.InstrumentationServiceMessageaction,config[LocalSettings.ServiceConfigurationKeys.InstrumentationServiceMessageaction]);
            }
        }

        #endregion
    }
}

我应该如何添加所需的处理程序?

wuzhixu5282 回答:Azure功能未注册身份验证处理程序

似乎某些代码正在尝试利用AspNetCore.Authentication.Core模块进行身份验证。使用身份验证时,由用户选择身份验证提供程序。在这种情况下,startup.cs文件中不会调用AddAuthentication()命令。有关此问题的更多文档,请参见:https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x?view=aspnetcore-3.0#authentication-middleware-and-services

在此特定情况下,以下代码被标识为对Authentication模块的调用:

builder.Services.AddMvc().AddFluentValidation(fv =>
                    fv.RegisterValidatorsFromAssemblyContaining<
                        Application.Portfolio.Commands.AssignSinglePortfolio.Validator>());
                builder.Services.AddTransient(typeof(IPipelineBehavior<,>),typeof(RequestValidationBehaviour<,>));

由于目标是匿名身份验证,因此已将其注释掉以解决问题。

本文链接:https://www.f2er.com/3160092.html

大家都在问