стандартная авторизация через клаймс работает на ура. но мне надо для совместимости старого сделать авторизацию по определенным токенам, не jwt.
так регистрирую
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
public IServiceProvider ConfigureServices(IServiceCollection services, IConfiguration configuration){
.....
services.AddAuthentication(options =>
{
options.DefaultScheme = AuthenticationDefaults.TokenScheme;
}).AddToken(services);
.....
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
....
app.UseAuthentication();
...
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
public static AuthenticationBuilder AddToken(this AuthenticationBuilder builder, IServiceCollection services)
{
return builder.AddScheme<TokenAuthenticationOptions, TokenAuthenticationHandler>(
AuthenticationDefaults.TokenAuthenticationScheme, options =>
{
var provider = services.BuildServiceProvider();
options.Logger = provider.GetService<ILogger>();
options.CustomerService= provider.GetService<ICustomerService>();
options.AuthenticationProcess = provider.GetService<IAuthenticationProcess>();
}
);
}
опция с прокинутыми сервисами
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
public class TokenAuthenticationOptions: AuthenticationSchemeOptions
{
public IAuthenticationProcess AuthenticationProcess { get; set; }
public ICustomerService CustomerService{ get; set; }
public ILogger Logger { get; set; }
public override void Validate()
{
if (LicKeyAuthenticationProcess == null)
throw new NullReferenceException($"{nameof(AuthenticationProcess)} is null");
if (ServiceUserRepository == null)
throw new NullReferenceException($"{nameof(CustomerService)} is null");
if (Logger == null)
throw new NullReferenceException($"{nameof(Logger)} is null");
}
}
сам хедлер
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
public class TokenAuthenticationHandler : AuthenticationHandler<TokenAuthenticationOptions>
{
protected TokenAuthenticationHandler (IOptionsMonitor<TokenAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
тут код логики
}
}
схему ставлю руками на контролере
1.
2.
3.
[Authorize(AuthenticationSchemes = AuthenticationDefaults.TokenAuthenticationScheme)]
public class SomeController : Controller{
}
в итоге получаю No authentication handler is configured to authenticate for the scheme.
Что я пропустил в регистрации?