我正在尝试创建一个简单的V2 Azure函数。它可以是匿名身份验证级别,因为它将由API管理保护。
构造函数和函数如下所示:
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
}
}
我应该如何添加所需的处理程序?
看起来有些代码试图利用AspNetCore.Authentication.Core模块进行身份验证。使用身份验证时,由用户选择身份验证提供程序。在这种情况下,在startup.cs文件中不会调用AddAuthentication((命令。有关此方面的更多文档,请访问:https://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x?view=aspnetcore-3.0#认证中间件和服务
在这个特定的例子中,以下代码被识别为调用身份验证模块:
builder.Services.AddMvc().AddFluentValidation(fv =>
fv.RegisterValidatorsFromAssemblyContaining<
Application.Portfolio.Commands.AssignSinglePortfolio.Validator>());
builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestValidationBehaviour<,>));
由于目标是匿名身份验证,因此已对此进行了注释以解决问题。