TechnitiumSoftware/DnsServer · error · HttpApiClientException

No active session exists. Please login and try again.

Error message

No active session exists. Please login and try again.

What it means

GetDashboardStatsAsync requires authentication. It checks _loggedIn at method entry and throws this HttpApiClientException if no session was established (neither LoginAsync nor UseApiToken succeeded). The guard fires before the dashboard request is built or sent.

Source

Thrown at DnsServerCore.HttpApi/HttpApiClient.cs:235

            CheckResponseStatus(rootElement);

            _httpClient.DefaultRequestHeaders.Remove("Authorization");
            _loggedIn = false;
        }

        public void UseApiToken(string token)
        {
            if (_loggedIn)
                throw new HttpApiClientException("Already logged in. Please create a new object to use a different API token.");

            _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            _loggedIn = true;
        }

        public async Task<DashboardStats> GetDashboardStatsAsync(string actingUsername, DashboardStatsType type = DashboardStatsType.LastHour, bool utcFormat = false, string acceptLanguage = "en-US,en;q=0.5", bool dontTrimQueryTypeData = false, DateTime startDate = default, DateTime endDate = default, CancellationToken cancellationToken = default)
        {
            if (!_loggedIn)
                throw new HttpApiClientException("No active session exists. Please login and try again.");

            string path = $"api/dashboard/stats/get?actingUser={Uri.EscapeDataString(actingUsername)}&type={type}&utc={utcFormat}&dontTrimQueryTypeData={dontTrimQueryTypeData}";

            if (type == DashboardStatsType.Custom)
                path += $"&start={startDate:O}&end={endDate:O}";

            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, new Uri(_serverUrl, path));
            httpRequest.Headers.Add("Accept-Language", acceptLanguage);

            HttpResponseMessage httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken);

            httpResponse.EnsureSuccessStatusCode();

            using JsonDocument jsonDoc = await JsonDocument.ParseAsync(httpResponse.Content.ReadAsStream(cancellationToken), cancellationToken: cancellationToken);
            JsonElement rootElement = jsonDoc.RootElement;

            CheckResponseStatus(rootElement);

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Call LoginAsync (or UseApiToken) once before invoking any dashboard/setting method.
  2. Centralise the login step in a helper that all callers go through.
  3. Handle the exception by re-authenticating and retrying once.

Example fix

// before
var client = new HttpApiClient(url);
var stats = await client.GetDashboardStatsAsync(user); // throws

// after
var client = new HttpApiClient(url);
await client.LoginAsync(user, pass);
var stats = await client.GetDashboardStatsAsync(user);
Defensive patterns

Strategy: validation

Validate before calling

if (!client.IsLoggedIn)
    await client.LoginAsync(user, pass); // or UseApiToken(token)

var stats = await client.GetDashboardStatsAsync(user);

Try / catch

catch (HttpApiClientException ex) when (ex.Message == "No active session exists. Please login and try again.")
{
    await client.LoginAsync(user, pass); // re-auth, retry once
    stats = await client.GetDashboardStatsAsync(user);
}

Prevention

When it happens

Trigger: Calling GetDashboardStatsAsync on a new HttpApiClient without first logging in or applying a token; or after the session lapsed/expired client-side.

Common situations: Forgetting the LoginAsync/UseApiToken step in a script; session was cleared by an exception path but the caller kept using the same client.

Related errors


AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13). Data as JSON: /api/errors/424fcbd60169e898. Report an issue: GitHub.