Why can I no longer access an external API when I log out of the remote session?

54 Views Asked by At

I'm running a tool (written in C# and I use Restsharp) in a Virtual Machine (with Windows server). This tool accesses to an external API and it works so far, but if at some point I access the VM via the remote session (Software still working) and then I end the session, accessing the API later no longer works. The tool is a console application and runs as scheduled task, its specified to run even without a logged on user.

The base URL of this API is: https://services.nvd.nist.gov/rest/json/cves/2.0

This is a part of the software (some lines are removed):

try
{
    do
    {
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        string url = mainUrl + $"resultsPerPage={resultsPerPage}&startIndex={startIndex}&" + parameter;

        WebProxy proxy = new WebProxy("x.x.x.x", xxxx);

        RestClientOptions options = new RestClientOptions()
        {
            Proxy = proxy,
            MaxTimeout = 35000,
            BaseUrl = new Uri(url),
            UseDefaultCredentials = false

        };                    

        var client = new RestClient(options);
        var request = new RestRequest();

        request.AddHeader("api-Key", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");                   

        var response = await client.ExecuteAsync(request);

        if (response.IsSuccessful)
        {
            // Read result as string
            string eventContent = response.Content;

            // Parse string in JArray
            parsedResult = JObject.Parse(eventContent);
            debugString = parsedResult.ToString();

            ...
            ...
            ...
            ...
            
            if (parsedResult["totalResults"].Value<int>() <= startIndex)
            {
                runTask = false;
            }
            }
            else
            {
                runTask = false;
            }

            await Task.Delay(7000);
        }
        else
        {
            DebugLogger.Instance.add(response.StatusCode + "(" + response.ErrorMessage + ")");
        }

    } while (runTask == true);

    return xxxxxxxx;
}
catch (Exception ex)
{
        DebugLogger.Instance.add(ex.ToString());
    throw ex;
}

# I did the following steps with a service account:

  1. Set a scheduled Task for the Tool to run on start up
  2. Restart the server
  3. The Server is restarted => The API Access works as expected
  4. Access to the Server through remote session => The API Access still works as expected
  5. I exit the remote session => The API access does not work anymore

As error message I get: "A task was canceled"

Do you know why and how can I solve this problem?

0

There are 0 best solutions below