title: Validating Azure AD JWT in C#
body: <p>Validate the azure AD jwt from your C# code by extracting the appid/clientid from the token. The below mentioned code will take token as string and validate your client id with the clientid/appid extracted from the token</p>
  
  <pre><code>public bool Validate(string token)
      {
          string clientId = "Your appid/clientid";
          string stsDiscoveryEndpoint = "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration";                      
          ConfigurationManager&lt;OpenIdConnectConfiguration&gt; configManager = new ConfigurationManager&lt;OpenIdConnectConfiguration&gt;(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;
          }      
  
      }
  </code></pre>