I have a task to authenticate the APIs using ADFS Token which is from an external application, so I have created two applications one is MVC application lets say A which authenticates using with SSO credentials and another one is WEB API application lets say B, So here FROM A, I am calling B's API using A's ADFS Token but, I am getting error. Does anyone help me how to resolve this?
Below is code in WEB API in application B
ConfigurationManager<OpenIdConnectConfiguration> configManager =
new ConfigurationManager<OpenIdConnectConfiguration>(openIdConfig, new
OpenIdConnectConfigurationRetriever());
OpenIdConnectConfiguration config =
configManager.GetConfigurationAsync().GetAwaiter().GetResult();
result.EmailId = Claims.FirstOrDefault(claim => claim.Type == "upn").Value;
result.WindowsNTId = Claims.FirstOrDefault(claim => claim.Type == "unique_name").Value;
var utc0 = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
result.TokenCreatedOn = utc0.AddSeconds(Convert.ToInt64((Claims.FirstOrDefault(claim =>
claim.Type == "iat").Value)));
result.TokenExpiresOn = utc0.AddSeconds(Convert.ToInt64((Claims.FirstOrDefault(claim =>
claim.Type == "exp").Value)));
// Use System.IdentityModel.Tokens.Jwt library to validate the token
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
var tokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidIssuer = config.Issuer,
IssuerSigningKeys = config.SigningKeys,
ValidateAudience = true,
ValidAudience = expectedAudience
};
SecurityToken validatedToken;
try
{
var claimsPrincipal = tokenHandler.ValidateToken(RawData, tokenValidationParameters,
out validatedToken);
}
catch (Exception ex)
{
}
Below is the Exception Message.
IDX10501: Signature validation failed. Unable to match key:
kid: 'System.String'.
Exceptions caught:System.Text.StringBuilder'.
token: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'.