Skip to content

Instantly share code, notes, and snippets.

@DavidKlempfner
Last active December 4, 2023 11:33
Show Gist options
  • Save DavidKlempfner/9fcc19e296964434cc111a4d3df30cd9 to your computer and use it in GitHub Desktop.
Save DavidKlempfner/9fcc19e296964434cc111a4d3df30cd9 to your computer and use it in GitHub Desktop.
PkceGeneration
private async Task HandleChallengeAsyncInternal(AuthenticationProperties properties)
{
// code omitted
if (Options.UsePkce && Options.ResponseType == OpenIdConnectResponseType.Code)
{
var bytes = new byte[32];
RandomNumberGenerator.Fill(bytes);
var codeVerifier = Base64UrlTextEncoder.Encode(bytes);
// Store this for use during the code redemption. See RunAuthorizationCodeReceivedEventAsync.
properties.Items.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
var challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
var codeChallenge = WebEncoders.Base64UrlEncode(challengeBytes);
message.Parameters.Add(OAuthConstants.CodeChallengeKey, codeChallenge);
message.Parameters.Add(OAuthConstants.CodeChallengeMethodKey, OAuthConstants.CodeChallengeMethodS256);
}
// code omitted
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment