Friday, December 04, 2020

C# Code for Validating Azure B2C Token

string tenantId = "FA368AEE-7911-41FA-9E16-D5B6A663777D";
string clientId = "2906B74F-96A9-45AA-BA1B-5175830991AF";
string validIssuer = $"https://contoso.b2clogin.com/{tenantId}/v2.0/";
string keysIssuer = "https://contoso.b2clogin.com/contoso.onmicrosoft.com/B2C_1_signup_signin/v2.0/.well-known/openid-configuration";

var signingKeys = new ConfigurationManager<OpenIdConnectConfiguration>(keysIssuer, new OpenIdConnectConfigurationRetriever()).GetConfigurationAsync().Result;

SecurityToken securityToken;
var tokenHandler = new JwtSecurityTokenHandler();
var claimsPrinciple = tokenHandler.ValidateToken(token, new TokenValidationParameters() { 
	ValidIssuer = validIssuer,
	IssuerSigningKeys = signingKeys.SigningKeys,
	ValidateAudience = true,
	ValidAudience = clientId
}, out securityToken);

No comments:

Post a Comment