Это было просто, я отвечу здесь, если кто-то ищет:
В основном просто нужно определить пользовательский AuthenticationHandler :
public class DynamicAuthenticationOptions : AuthenticationSchemeOptions
{
}
public class DynamicAuthenticationHandler : AuthenticationHandler<DynamicAuthenticationOptions>
{
public DynamicAuthenticationHandler(
IOptionsMonitor<DynamicAuthenticationOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock
)
: base(options, logger, encoder, clock)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Request.Headers.ContainsKey("Authorization"))
return Task.FromResult(AuthenticateResult.Fail("Unauthorized"));
string authorizationHeader = Request.Headers["Authorization"];
if (string.IsNullOrEmpty(authorizationHeader))
return Task.FromResult(AuthenticateResult.Fail("Unauthorized"));
if (!authorizationHeader.StartsWith("bearer", StringComparison.OrdinalIgnoreCase))
return Task.FromResult(AuthenticateResult.Fail("Unauthorized"));
var token = authorizationHeader.Substring("bearer".Length).Trim();
IEnumerable<Claim> claims;
foreach (var validMergedSchema in validMergedSchemas)
{
if (IsTokenValid(token, validMergedSchema, out claims))
{
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new System.Security.Principal.GenericPrincipal(identity, null);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}
return Task.FromResult(AuthenticateResult.Fail("Unauthorized"));
}
private bool IsTokenValid(string tokenStr, string schema, out IEnumerable<Claim> claims)
{
claims = null;
switch (schema.ToLower())
{
case "schema1":
return IsTokenValidBySchema1(tokenStr, out claims);
case "schema2":
return IsTokenValidBySchema2(tokenStr, out claims);
case "schema3":
return IsTokenValidBySchema3(tokenStr, out claims);
default:
return false;
}
}
}