{"record":{"id":"424fcbd60169e898","repo":"TechnitiumSoftware/DnsServer","slug":"no-active-session-exists-please-login-and-try-aga","errorCode":null,"errorMessage":"No active session exists. Please login and try again.","messagePattern":"No active session exists\\. Please login and try again\\.","errorType":"exception","errorClass":"HttpApiClientException","httpStatus":null,"severity":"error","filePath":"DnsServerCore.HttpApi/HttpApiClient.cs","lineNumber":235,"sourceCode":"            CheckResponseStatus(rootElement);\n\n            _httpClient.DefaultRequestHeaders.Remove(\"Authorization\");\n            _loggedIn = false;\n        }\n\n        public void UseApiToken(string token)\n        {\n            if (_loggedIn)\n                throw new HttpApiClientException(\"Already logged in. Please create a new object to use a different API token.\");\n\n            _httpClient.DefaultRequestHeaders.Add(\"Authorization\", \"Bearer \" + token);\n            _loggedIn = true;\n        }\n\n        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)\n        {\n            if (!_loggedIn)\n                throw new HttpApiClientException(\"No active session exists. Please login and try again.\");\n\n            string path = $\"api/dashboard/stats/get?actingUser={Uri.EscapeDataString(actingUsername)}&type={type}&utc={utcFormat}&dontTrimQueryTypeData={dontTrimQueryTypeData}\";\n\n            if (type == DashboardStatsType.Custom)\n                path += $\"&start={startDate:O}&end={endDate:O}\";\n\n            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, new Uri(_serverUrl, path));\n            httpRequest.Headers.Add(\"Accept-Language\", acceptLanguage);\n\n            HttpResponseMessage httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken);\n\n            httpResponse.EnsureSuccessStatusCode();\n\n            using JsonDocument jsonDoc = await JsonDocument.ParseAsync(httpResponse.Content.ReadAsStream(cancellationToken), cancellationToken: cancellationToken);\n            JsonElement rootElement = jsonDoc.RootElement;\n\n            CheckResponseStatus(rootElement);\n","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore.HttpApi/HttpApiClient.cs#L217-L253","documentation":"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.","triggerScenarios":"Calling GetDashboardStatsAsync on a new HttpApiClient without first logging in or applying a token; or after the session lapsed/expired client-side.","commonSituations":"Forgetting the LoginAsync/UseApiToken step in a script; session was cleared by an exception path but the caller kept using the same client.","solutions":["Call LoginAsync (or UseApiToken) once before invoking any dashboard/setting method.","Centralise the login step in a helper that all callers go through.","Handle the exception by re-authenticating and retrying once."],"exampleFix":"// before\nvar client = new HttpApiClient(url);\nvar stats = await client.GetDashboardStatsAsync(user); // throws\n\n// after\nvar client = new HttpApiClient(url);\nawait client.LoginAsync(user, pass);\nvar stats = await client.GetDashboardStatsAsync(user);","handlingStrategy":"validation","validationCode":"if (!client.IsLoggedIn)\n    await client.LoginAsync(user, pass); // or UseApiToken(token)\n\nvar stats = await client.GetDashboardStatsAsync(user);","typeGuard":null,"tryCatchPattern":"catch (HttpApiClientException ex) when (ex.Message == \"No active session exists. Please login and try again.\")\n{\n    await client.LoginAsync(user, pass); // re-auth, retry once\n    stats = await client.GetDashboardStatsAsync(user);\n}","preventionTips":["Always authenticate before any data-fetching call.","Centralise authenticated-client creation in a helper.","Implement a one-shot re-auth-and-retry wrapper for session-expiry cases."],"tags":["http-api","client","session","dashboard","auth"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}