Build some sort of identity provider service that can provide the hash and salt of a user's password. Then use the following as an authentication provider:
public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly IUserService userService;
public BasicAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
IUserService userService) : base(options, logger, encoder, clock)
{
this.userService = userService;
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Request.Headers.ContainsKey("Authorization"))
return Task.FromResult(AuthenticateResult.Fail("Missing Authorization Header"));
try
{
AuthenticationHeaderValue? authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
byte[]? credentialBytes = Convert.FromBase64String(authHeader.Parameter);
string[]? credentials = Encoding.UTF8.GetString(credentialBytes).Split(new char[] { ':' }, 2);
string username = credentials[0];
string password = credentials[1];
User? user = userService.Authenticate(username, password);
if (user is null)
return Task.FromResult(AuthenticateResult.Fail("Invalid username or password"));
IEnumerable<Claim> claims = new Claim[]
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Name, user.Username)
};
ClaimsIdentity identity = new ClaimsIdentity(claims, Scheme.Name);
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
AuthenticationTicket ticket = new AuthenticationTicket(principal, Scheme.Name);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
catch
{
return Task.FromResult(AuthenticateResult.Fail("Invalid Authorization header"));
}
}
}
Then add the following to startup:
//Configure Services Section
services.AddAuthentication("BasicAuthentication")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);
//Configure Section
app.UseAuthentication();
app.UseAuthorization();