Following this article, I am trying to implement a custom AuthenticationHandler, but I got stuck on dependency injection.
I need to inject an IRepository instance into the AuthenticationHandler to provide a dbo connection (to check the credentials).
Code:
public class CustomAuthenticationHandler : AuthenticationHandler<CustomAuthenticationOptions>
{
// how to inject this?!
private IRepository repository;
public CustomAuthenticationHandler(IOptionsMonitor<CustomAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock) {
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
// this is just a sample
if (repository.users.Count(w => w.user_name == Request.Headers["user_name"] && w.password == Request.Headers["password"]) == 1)
{
return Task.FromResult(
AuthenticateResult.Success(
new AuthenticationTicket(
new ClaimsPrincipal(Options.Identity),
new AuthenticationProperties(),
this.Scheme.Name)));
}
return Task.FromResult(
AuthenticateResult.Failed("...");
);
}
Do you have any hints?
Thanks
authenticationrather thanauthorizationI'm afraid @Fals. Similar ballpark but not the same thing.