简体   繁体   中英

Getting error OWIN start.cs while web API hosting on prem IIS server and api having azure ad bearer authentication

I have implemented azure ad authentication in SPA app and validating the token using OWIN start.cs. it is working fine in local environment. below code used for token validation:

    app.UseWindowsAzureActiveDirectoryBearerAuthentication(new WindowsAzureActiveDirectoryBearerAuthenticationOptions
    {
        Tenant = tenant,
        TokenValidationParameters = new TokenValidationParameters
        {
            //ValidAudiences = new[] { Audience },
            ValidAudience = validateAudience,
            ValidateIssuer = false,
            ValidIssuers = new[] { ValidIssuers }
        }
    });

when I deployed this published code on prem server and run web api it throws error as mentioned in below screen shot. I have used Microsoft.Owin.Security.ActiveDirectory version 4.2.2.0, 在此处输入图像描述 在此处输入图像描述

can anyone help on this ?

Basically, the error task was canceled is expected when an HTTP request is canceled maybe if the user closes the page suddenly. or cancels before execution completes.

The TaskCanceledException is mostly timeout.But to check the exception, it may have to be inspected with try catch.

try
{
    var response = task.Result;
}
catch (TaskCanceledException ex)
{
    // Check ex.CancellationToken.IsCancellationRequested here.
    // If false, we may assume it was a timeout in most cases.
}

you can try to suppress response if the cancellation token occurred by using if (cancellationToken.IsCancellationRequested)

 HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
        if (cancellationToken.IsCancellationRequested)
        {
            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }
        return response;

If it is due to time out ,it can be set to some value.Plesae check the references.

var clientHttp = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(30)

In some cases of use , await can be used for simultaneous tasks to be completed with async.

References:

  1. c# - "Task was canceled" errors on Azure website - Stack Overflow
  2. c# - HttpClient - A task was cancelled? - Stack Overflow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM