Validating Azure AD JWT in C# -
validate azure ad jwt c# code extracting appid/clientid token. below mentioned code take token string , validate client id clientid/appid extracted token
public bool validate(string token) { string clientid = "your appid/clientid"; string stsdiscoveryendpoint = "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"; configurationmanager<openidconnectconfiguration> configmanager = new configurationmanager<openidconnectconfiguration>(stsdiscoveryendpoint, new openidconnectconfigurationretriever()); openidconnectconfiguration config = configmanager.getconfigurationasync().result; tokenvalidationparameters validationparameters = new tokenvalidationparameters { validateaudience = false, validateissuer = false, issuersigningkey = config.signingkeys.firstordefault(), validatelifetime = false }; system.identitymodel.tokens.jwt.jwtsecuritytokenhandler tokendhandler = new jwtsecuritytokenhandler(); securitytoken jwt; var result = tokendhandler.validatetoken(token, validationparameters, out jwt); claimsprincipal claims = result; var claimvalue = claims.claims.getenumerator(); string appid = ""; if (claimvalue != null) { while (claimvalue.movenext()) { if (claimvalue.current.type == "appid") { appid = claimvalue.current.value; break; } } } if (appid == clientid) { return true; } else { return false; } }
Comments
Post a Comment