Azure AD login returning unauthorize (401) inspite how having upto-date config's
09:28 14 Jan 2026

as per title i am trying to integrete azure-AD login in my .net 4.8 application with angular frontend. but when i try to authenicate its returning 401 unauthorize error. and in controller all the claim is missing and controller returning 500 internal server error.

ill paste the code here.

```
            app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                AllowedAudiences = new[] { clientId },
                TokenValidationParameters = new TokenValidationParameters
                {
                    ValidAudience = clientId,
                    ValidateAudience = true,

                    ValidIssuer = issuer,
                    ValidateIssuer = true,

                    ValidateIssuerSigningKey = true,
                    ValidateLifetime = true,

                    ClockSkew = TimeSpan.FromMinutes(5)
                },
                IssuerSecurityTokenProviders = new[]
                {
                    new OpenIdConnectCachingSecurityTokenProvider(
                        $"{authority}/.well-known/openid-configuration")
                }
            });
    }


    public OpenIdConnectCachingSecurityTokenProvider(string metadataEndpoint)
{
    var wc = new WebClient();
    var metadata = JObject.Parse(wc.DownloadString(metadataEndpoint));
    Issuer = metadata["issuer"].ToString();

    var jwksUri = metadata["jwks_uri"].ToString();
    var keysJson = wc.DownloadString(jwksUri);
    var jwks = JObject.Parse(keysJson);

    var tokens = new List();

    foreach (var key in jwks["keys"])
    {
        var x5cArray = key["x5c"] as JArray;
        if (x5cArray == null || x5cArray.Count == 0) continue;

        var certString = x5cArray[0].ToString();
        var certBytes = Convert.FromBase64String(certString);
        var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(certBytes);
        var securityToken = new X509SecurityToken(cert);

        tokens.Add(securityToken);
    }

    SecurityTokens = tokens;
}




```
c# .net authentication azure-active-directory claims