Last active
August 9, 2022 04:06
-
-
Save Mgs25/7936f449ace5296edfee0371e4711b6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions> | |
{ | |
public BasicAuthenticationHandler( | |
IOptionsMonitor<AuthenticationSchemeOptions> options, | |
ILoggerFactory logger, | |
UrlEncoder encoder, | |
ISystemClock clock | |
) : base(options, logger, encoder, clock) | |
{ | |
} | |
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() | |
{ | |
string username = null; | |
try | |
{ | |
var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); | |
var credentials = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter)).Split(':'); | |
username = credentials.FirstOrDefault(); | |
var password = credentials.LastOrDefault(); | |
} | |
catch (Exception ex) | |
{ | |
return AuthenticateResult.Fail($"Auth failed: {ex.Message}"); | |
} | |
var claims = new[] { | |
new Claim(ClaimTypes.Name, username) | |
}; | |
var identity = new ClaimsIdentity(claims, Scheme.Name); | |
var principal = new ClaimsPrincipal(identity); | |
var ticket = new AuthenticationTicket(principal, Scheme.Name); | |
return AuthenticateResult.Success(ticket); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment