How to validate Azure AD security token? How to validate Azure AD security token? azure azure

How to validate Azure AD security token?


There are two steps to verify the token. First, verify the signature of the token to ensure the token was issued by Azure Active Directory. Second, verify the claims in the token based on the business logic.

For example, we need to verify the iss and aud claim if you were developing a single tenant app. And you also need to verify the nbf to ensure the token is not expired. More claims you can refer here.

Below description is from here about the detail of signature verifying. (Note: The example below uses the Azure AD v2 endpoint. You should use the endpoint that corresponds to the endpoint the client app is using.)

The access token from the Azure AD is a JSON Web Token(JWT) which is signed by Security Token Service in private key.

The JWT includes 3 parts: header, data, and signature. Technically, we can use the public key to validate the access token.

First step – retrieve and cache the singing tokens (public key)

Endpoint: https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration

Then we can use the JwtSecurityTokenHandler to verify the token using the sample code below:

 public JwtSecurityToken Validate(string token) {     string stsDiscoveryEndpoint = "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration";     ConfigurationManager<OpenIdConnectConfiguration> configManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint);     OpenIdConnectConfiguration config = configManager.GetConfigurationAsync().Result;     TokenValidationParameters validationParameters = new TokenValidationParameters     {         ValidateAudience = false,         ValidateIssuer = false,         IssuerSigningTokens = config.SigningTokens,         ValidateLifetime = false     };     JwtSecurityTokenHandler tokendHandler = new JwtSecurityTokenHandler();     SecurityToken jwt;     var result = tokendHandler.ValidateToken(token, validationParameters, out jwt);     return jwt as JwtSecurityToken; }

And if you were using the OWIN components in your project, it is more easy to verify the token. We can use the code below to verify the token:

app.UseWindowsAzureActiveDirectoryBearerAuthentication(            new WindowsAzureActiveDirectoryBearerAuthenticationOptions            {                Audience = ConfigurationManager.AppSettings["ida:Audience"],                Tenant = ConfigurationManager.AppSettings["ida:Tenant"]            });

Then we can use the code below to verify the ‘scope’ in the token:

public IEnumerable<TodoItem> Get(){    // user_impersonation is the default permission exposed by applications in AAD    if (ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/scope").Value != "user_impersonation")    {        throw new HttpResponseException(new HttpResponseMessage {          StatusCode = HttpStatusCode.Unauthorized,          ReasonPhrase = "The Scope claim does not contain 'user_impersonation' or scope claim not found"        });    }    ...}

And here is a code sample which protected the web API with Azure AD:

Protect a Web API using Bearer tokens from Azure AD


Just wanted to add to Fei's answer for people using .net Core 2.0

You'll have to modify 2 lines of the Validate(string token) method.

 var configManager = new ConfigurationManager<OpenIdConnectConfiguration>(        stsDiscoveryEndpoint,        new OpenIdConnectConfigurationRetriever()); //1. need the 'new OpenIdConnect...' OpenIdConnectConfiguration config = configManager.GetConfigurationAsync().Result; TokenValidationParameters validationParameters = new TokenValidationParameters {     //decode the JWT to see what these values should be     ValidAudience = "some audience",     ValidIssuer = "some issuer",     ValidateAudience = true,     ValidateIssuer = true,     IssuerSigningKeys = config.SigningKeys, //2. .NET Core equivalent is "IssuerSigningKeys" and "SigningKeys"     ValidateLifetime = true };


But if you are not using OWIN in your projects, it is going to be a little hard or at least time consuming..This articleHere is great resource.

And because I do not have much to add on the above, except the detailed code.. Here is something that can be useful to you:

 public async Task<ClaimsPrincipal> CreatePrincipleAsync()    {        AzureActiveDirectoryToken azureToken = Token.FromJsonString<AzureActiveDirectoryToken>();        var allParts = azureToken.IdToken.Split(".");        var header = allParts[0];        var payload = allParts[1];        var idToken = payload.ToBytesFromBase64URLString().ToAscii().FromJsonString<AzureActiveDirectoryIdToken>();        allParts = azureToken.AccessToken.Split(".");        header = allParts[0];        payload = allParts[1];        var signature = allParts[2];        var accessToken = payload.ToBytesFromBase64URLString().ToAscii().FromJsonString<AzureActiveDirectoryAccessToken>();        var accessTokenHeader = header.ToBytesFromBase64URLString().ToAscii().FromJsonString<AzureTokenHeader>();        var isValid = await ValidateToken(accessTokenHeader.kid, header, payload, signature);        if (!isValid)        {            throw new SecurityException("Token can not be validated");        }        var principal = await CreatePrincipalAsync(accessToken, idToken);        return principal;    }    private async Task<bool> ValidateToken(string kid, string header, string payload, string signature)    {        string keysAsString = null;        const string microsoftKeysUrl = "https://login.microsoftonline.com/common/discovery/keys";        using (var client = new HttpClient())        {            keysAsString = await client.GetStringAsync(microsoftKeysUrl);        }        var azureKeys = keysAsString.FromJsonString<MicrosoftConfigurationKeys>();        var signatureKeyIdentifier = azureKeys.Keys.FirstOrDefault(key => key.kid.Equals(kid));        if (signatureKeyIdentifier.IsNotNull())        {            var signatureKey = signatureKeyIdentifier.x5c.First();            var certificate = new X509Certificate2(signatureKey.ToBytesFromBase64URLString());            var rsa = certificate.GetRSAPublicKey();            var data = string.Format("{0}.{1}", header, payload).ToBytes();            var isValidSignature = rsa.VerifyData(data, signature.ToBytesFromBase64URLString(), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);            return isValidSignature;        }        return false;    }

There are some functions that I use in here that are not available for you, they are self descriptive.