{"record":{"id":"596b51af73e879d1","repo":"microsoft/semantic-kernel","slug":"please-provide-valid-msgraph-configuration-in-apps","errorCode":null,"errorMessage":"Please provide valid MSGraph configuration in appsettings.Development.json file.","messagePattern":"Please provide valid MSGraph configuration in appsettings\\.Development\\.json file\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"dotnet/samples/Demos/CopilotAgentPlugins/CopilotAgentPluginsDemoSample/BearerAuthenticationProviderWithCancellationToken.cs","lineNumber":27,"sourceCode":"/// \"bearer\" authentication scheme.\n/// </summary>\npublic class BearerAuthenticationProviderWithCancellationToken\n{\n    private readonly IPublicClientApplication _client;\n\n    /// <summary>\n    /// Creates an instance of the <see cref=\"BearerAuthenticationProviderWithCancellationToken\"/> class.\n    /// </summary>\n    /// <param name=\"configuration\">The configuration instance to read settings from.</param>\n    public BearerAuthenticationProviderWithCancellationToken(IConfiguration configuration)\n    {\n        ArgumentNullException.ThrowIfNull(configuration);\n        var clientId = configuration[\"MSGraph:ClientId\"];\n        var tenantId = configuration[\"MSGraph:TenantId\"];\n\n        if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(tenantId))\n        {\n            throw new InvalidOperationException(\"Please provide valid MSGraph configuration in appsettings.Development.json file.\");\n        }\n\n        this._client = PublicClientApplicationBuilder\n            .Create(clientId)\n            .WithAuthority($\"https://login.microsoftonline.com/{tenantId}\")\n            .WithDefaultRedirectUri()\n            .Build();\n    }\n\n    /// <summary>\n    /// Applies the token to the provided HTTP request message.\n    /// </summary>\n    /// <param name=\"request\">The HTTP request message.</param>\n    /// <param name=\"cancellationToken\"></param>\n    public async Task AuthenticateRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken = default)\n    {\n        var token = await this.GetAccessTokenAsync(cancellationToken).ConfigureAwait(false);\n        request.Headers.Authorization = new AuthenticationHeaderValue(\"Bearer\", token);","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/CopilotAgentPlugins/CopilotAgentPluginsDemoSample/BearerAuthenticationProviderWithCancellationToken.cs#L9-L45","documentation":"BearerAuthenticationProviderWithCancellationToken reads MSGraph:ClientId and MSGraph:TenantId from IConfiguration in its constructor and throws InvalidOperationException if either is null/empty. It then builds a PublicClientApplication, so this guard prevents MSAL from being constructed with invalid authority. The message points at appsettings.Development.json specifically because the sample loads development config there.","triggerScenarios":"Constructor invoked with an IConfiguration where configuration[\"MSGraph:ClientId\"] or [\"MSGraph:TenantId\"] is null or empty string.","commonSituations":"appsettings.Development.json missing or lacking the MSGraph section, the section present but under a different key (e.g. AzureAd:ClientId), or the Development environment not active so the file isn't loaded.","solutions":["Add an MSGraph section with ClientId and TenantId to appsettings.Development.json.","Confirm ASPNETCORE_ENVIRONMENT (or equivalent) is Development so the file is loaded.","Verify the JSON key names exactly match MSGraph:ClientId / MSGraph:TenantId (case-sensitive path).","As a fallback set the same keys via environment variables (MSGraph__ClientId, MSGraph__TenantId)."],"exampleFix":"// before\nif (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(tenantId))\n    throw new InvalidOperationException(\"Please provide valid MSGraph configuration...\");\n\n// after (appsettings.Development.json)\n{\n  \"MSGraph\": { \"ClientId\": \"<app-id>\", \"TenantId\": \"<tenant-id>\" }\n}","handlingStrategy":"validation","validationCode":"var clientId = configuration[\"MSGraph:ClientId\"];\nvar tenantId = configuration[\"MSGraph:TenantId\"];\nif (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(tenantId))\n    throw new InvalidOperationException(\n        \"Missing MSGraph:ClientId/TenantId. Add them to appsettings.Development.json or env vars MSGraph__ClientId/MSGraph__TenantId.\");","typeGuard":"static bool HasMsGraphConfig(IConfiguration c) =>\n    !string.IsNullOrEmpty(c[\"MSGraph:ClientId\"]) && !string.IsNullOrEmpty(c[\"MSGraph:TenantId\"]);","tryCatchPattern":null,"preventionTips":["Keep a local config-health check that asserts required keys at startup.","Use environment variables for CI (MSGraph__ClientId / MSGraph__TenantId).","Confirm the Development environment is active so appsettings.Development.json loads."],"tags":["configuration","msgraph","authentication","samples"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}