Skip to content

Instantly share code, notes, and snippets.

@mt-akar
Created August 5, 2022 04:45
Show Gist options
  • Save mt-akar/b591b48ebc067fcc4f32ea5a14814413 to your computer and use it in GitHub Desktop.
Save mt-akar/b591b48ebc067fcc4f32ea5a14814413 to your computer and use it in GitHub Desktop.
This code checks key from url and secret from auth header password for basic auth againts the Client.ClientId as key and ClientSecret.Value as secret-hash.
try
{
var authHeader = Request.Headers.Authorization.ToString();
if (!authHeader.StartsWith("Basic "))
return Unauthorized();
var apiSecretHash = Encoding.ASCII.GetString(Convert.FromBase64String(authHeader[6..]))
.Split(':')[1]
.ToSha256();
var secretValid = await _configDb.Clients
.Include(c => c.ClientSecrets)
.Where(c => c.ClientId == apiKey)
.Select(c => c.ClientSecrets.Any(cs => cs.Value == apiSecretHash))
.FirstOrDefaultAsync();
if (!secretValid)
return Unauthorized();
}
catch
{
return Unauthorized();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment