Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException'
в Microsoft .IdentityModel.JsonWebTokens.dll.
Я настраиваю динамическую и настраиваемую систему аутентификации из файл конфигурации .JSON (ASP.NET 8).
Я вызываю статический метод в Program.cs, который загружает конфигурацию с помощью отражения.
Однако я столкнулся с проблемой с преобразование типа ключа (SymmetricKey).
Как управлять динамической настройкой опций этого типа или других типов?
Спасибо< /p>
Код: Выделить всё
public static class AuthenticationConfigurator
Код: Выделить всё
public static void ConfigureAuthentications(IServiceCollection services, IConfiguration configuration)
{
var listAuthenticationSchemes = configuration.GetSection("AuthenticationSchemes").Get();
var defaultScheme = configuration.GetValue("Authentication:DefaultScheme");
var authBuilder = services.AddAuthentication(defaultScheme);
foreach (var scheme in listAuthenticationSchemes)
{
var handlerType = Type.GetType(scheme.Handler);
if (handlerType is null)
throw new InvalidOperationException($"Handler type '{scheme.Handler}' not found for scheme '{scheme.Name}'.");
var optionsType = handlerType.BaseType?.GetGenericArguments()[0];
if (optionsType is null)
throw new InvalidOperationException($"Options type not found for handler '{handlerType.FullName}'.");
var addSchemeMethod = typeof(AuthenticationBuilder)
.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(method => String.Equals(method.Name, nameof(AuthenticationBuilder.AddScheme)) && method.IsGenericMethod && method.GetParameters().Length == 3)
?.MakeGenericMethod(optionsType, handlerType);
if (addSchemeMethod is null)
throw new InvalidOperationException("AddScheme method not found for authentication.");
addSchemeMethod.Invoke(authBuilder, new object[]
{
scheme.Name,
scheme.Name,
(Action)(options =>
{
if (scheme.Options is null) return;
foreach (var option in scheme.Options)
{
var property = optionsType.GetProperty(option.Key);
if (property is not null)
{
var convertedValue = Convert.ChangeType(option.Value, property.PropertyType);
property.SetValue(options, convertedValue);
}
}
})
});
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... -exception
Мобильная версия