{"record":{"id":"5037d831fa020b1c","repo":"microsoft/aspire","slug":"failed-to-authenticate-to-the-apphost-server-5037d8","errorCode":null,"errorMessage":"Failed to authenticate to the AppHost server.","messagePattern":"Failed to authenticate to the AppHost server\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Cli/Projects/AppHostRpcClient.cs","lineNumber":68,"sourceCode":"        try\n        {\n            var formatter = BackchannelJsonSerializerContext.CreateRpcMessageFormatter();\n            var handler = new HeaderDelimitedMessageHandler(stream, stream, formatter);\n            jsonRpc = new JsonRpc(handler)\n            {\n                ActivityTracingStrategy = new ActivityTracingStrategy()\n            };\n            jsonRpc.StartListening();\n\n            var authenticated = await jsonRpc.InvokeWithProfilingAsync<bool>(\n                profilingTelemetry,\n                ConnectionName,\n                \"authenticate\",\n                [authenticationToken],\n                cancellationToken).ConfigureAwait(false);\n            if (!authenticated)\n            {\n                throw new InvalidOperationException(\"Failed to authenticate to the AppHost server.\");\n            }\n\n            return new AppHostRpcClient(stream, jsonRpc, profilingTelemetry);\n        }\n        catch\n        {\n            jsonRpc?.Dispose();\n            await stream.DisposeAsync();\n            throw;\n        }\n    }\n\n    /// <inheritdoc />\n    public Task<RuntimeSpec> GetRuntimeSpecAsync(string languageId, CancellationToken cancellationToken)\n        => InvokeAsync<RuntimeSpec>(\"getRuntimeSpec\", [languageId], cancellationToken);\n\n    /// <inheritdoc />\n    public Task<Dictionary<string, string>> ScaffoldAppHostAsync(","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Cli/Projects/AppHostRpcClient.cs#L50-L86","documentation":"Thrown by AppHostRpcClient.ConnectAsync when the AppHost server rejects the CLI's authentication handshake — the RPC 'authenticate' call returned false even though the transport connected. The CLI refuses to create an AppHostRpcClient over an unauthenticated stream to prevent talking to an untrusted or mismatched server.","triggerScenarios":"Calling ConnectAsync when the authenticationToken sent via the 'authenticate' RPC does not match what the AppHost server expects, so the server returns false.","commonSituations":"Stale socket/token files left over from a previous session, a different AppHost server process owning the socket path, version mismatch between CLI and server, or another process squatting on the socket.","solutions":["Restart the AppHost/CLI session so a fresh authentication token is issued","Delete stale socket/token files in the temp/sockets directory and retry","Ensure CLI and AppHost server versions match (update or reinstall aspire)","Verify no other process is bound to the same socket path"],"exampleFix":"// before\naspire run  // reuses stale socket from crashed session\n// after\nrm -rf $TMPDIR/aspire.* && aspire run","handlingStrategy":"retry","validationCode":"if (!File.Exists(socketPath))\n    throw new InvalidOperationException(\"Socket path does not exist; start the server first.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var client = await AppHostRpcClient.ConnectAsync(...);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"authenticate\"))\n{\n    // discard stale session state and retry once with a fresh token\n}","preventionTips":["Clean up stale socket/token files from crashed sessions","Keep CLI and AppHost server versions aligned","Never reuse socket paths across security contexts","Re-run authentication flow rather than caching tokens"],"tags":["rpc","authentication","ipc"],"backgroundTag":"authentication-required","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}