we are authenticating an web API using azure ad JWT token which is deployed on separate server from UI server. In local environment it is working fine. but on server when we request for API call from UI server using azure ad bearer token, API throws error. we are getting error like : 401 (Unauthorized), we logged the stack trace also like below
token validation start at:- 7/20/2022 1:37:14 PM
token validation failed: System.Threading.Tasks.TaskCanceledException: A task was canceled. stack trace: at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) at Microsoft.Owin.Security.ActiveDirectory.WsFedMetadataRetriever.GetSigningKeys(String metadataEndpoint, TimeSpan backchannelTimeout, HttpMessageHandler backchannelHttpHandler) at Microsoft.Owin.Security.ActiveDirectory.WsFedCachingSecurityKeyProvider.RetrieveMetadata() at Microsoft.Owin.Security.ActiveDirectory.WsFedCachingSecurityKeyProvider..ctor(String metadataEndpoint, ICertificateValidator backchannelCertificateValidator, TimeSpan backchannelTimeout, HttpMessageHandler backchannelHttpHandler) at Owin.WindowsAzureActiveDirectoryBearerAuthenticationExtensions.UseWindowsAzureActiveDirectoryBearerAuthentication(IAppBuilder app, WindowsAzureActiveDirectoryBearerAuthenticationOptions options) at KFF.Web.App_Start.Startup.Configuration(IAppBuilder app) in D:\Source Code\KONE Maintence\maintenance\KFF\BackEnd\WebApp\App_Start\Startup.cs:line 132 massage : One or more errors occurred.
Below is my code which I used to authenticate the user: try {
//used to validate azure ad bearer token
sb.Append("token validation start at:- " + DateTime.Now.ToString() + " \n");
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = tenant,
TokenValidationParameters = new TokenValidationParameters
{
ValidAudience = ValidAudiences,
ValidateIssuer = false,
// ValidIssuers = new[] { ValidIssuers }
//IssuerSigningKeys = openidConfiguration.SigningKeys,
//ValidateAudience = false
}
});
sb.Append("\n" + "token validation success:- " + DateTime.Now.ToString() + "\n" );
File.AppendAllText(logPath + "log.txt", sb.ToString());
sb.Clear();
}
catch (Exception ex)
{
sb.Append("\n" + "token validation failed: " + "\n" + ex.InnerException + "\n" + " stack trace: "+ "\n" + ex.StackTrace + "\n" + "massage :" + "\n" + ex.Message);
File.AppendAllText(logPath + "log.txt", sb.ToString());
sb.Clear();
}
API controller code is mentioned below having Authorize attribute.
[Authorize]
public class AuthorizationController : ApiController
{
/// <summary>
/// </summary>
[Route("")]
[HttpGet, HttpOptions]
public string GetToken(bool staylogged, bool useSalesforce = false)
{
var authHeader = HttpContext.Current.Request.Headers["Authorization"];
if (useSalesforce)
return AuthHelper.SalesforceLogin(authHeader, staylogged);
if (string.IsNullOrWhiteSpace(authHeader))
{
HttpContext.Current.Response.StatusCode = 401;
return null;
}
var jwt = authHeader.Replace("Bearer ", string.Empty);
//var token = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(jwt);
//authHeader = authHeader.Replace("Bearer", string.Empty);
return jwt;
}
Could any one guide me pls ?