{"record":{"id":"568379dd5b5cdc21","repo":"microsoft/ailab","slug":"a-subscription-key-is-required","errorCode":null,"errorMessage":"A subscription key is required","messagePattern":"A subscription key is required","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"BuildAnIntelligentBot/src/ChatBot/Models/AzureAuthToken.cs","lineNumber":20,"sourceCode":"\nnamespace ChatBot.Models\n{\n  /// <summary>\n  /// Class with the Cognitive Services Azure Auth Token information.\n  /// </summary>\n  public class AzureAuthToken\n  {\n    // When the last valid token was obtained.\n    private DateTime _storedTokenTime = DateTime.MinValue;\n\n    // Cache the value of the last valid token obtained from the token service.\n    private string _storedTokenValue = string.Empty;\n\n    public AzureAuthToken(string key, string tokenValue)\n    {\n      if (string.IsNullOrEmpty(key))\n      {\n        throw new ArgumentNullException(\"key\", \"A subscription key is required\");\n      }\n\n      SubscriptionKey = key;\n      StoredTokenValue = tokenValue;\n    }\n\n    // Gets the subscription key.\n    public string SubscriptionKey { get; private set; }\n\n    public string StoredTokenValue\n    {\n      get\n      {\n        return this._storedTokenValue;\n      }\n\n      set\n      {","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/microsoft/ailab/blob/89fe2fc62081145ec5408d42059cbcb7c4a033c8/BuildAnIntelligentBot/src/ChatBot/Models/AzureAuthToken.cs#L2-L38","documentation":"The AzureAuthToken constructor issues Cognitive Services auth tokens from a subscription key and refuses to run without one. If the key parameter is null or an empty string it throws ArgumentNullException with the message 'A subscription key is required'. Tokens cannot be requested from Azure without a valid key, so the class fails fast in the constructor.","triggerScenarios":"Instantiating new AzureAuthToken(null, ...) or new AzureAuthToken(\"\", ...) — typically when the key comes from appsettings.json or an environment variable that is missing or empty.","commonSituations":"Forgetting to add the Speech/Text Analytics subscription key to appsettings.json; key lookup returning empty (GetSection(...).Value is null); leaving the placeholder value empty in config; renaming the config key so the lookup misses.","solutions":["Provide a valid Cognitive Services subscription key when constructing AzureAuthToken(key, tokenValue)","Check appsettings.json / configuration for the key entry and that the lookup name matches","Null-check the configured value before constructing the class and surface a clear configuration error","Regenerate/copy the correct key from the Azure portal resource if it was blank"],"exampleFix":"// before\nvar auth = new AzureAuthToken(Configuration[\"AzureSubKey\"], null);\n// after\nvar key = Configuration[\"AzureSubKey\"];\nif (string.IsNullOrEmpty(key)) throw new InvalidOperationException(\"AzureSubKey is missing from configuration\");\nvar auth = new AzureAuthToken(key, null);","handlingStrategy":"validation","validationCode":"var key = Configuration[\"AzureSubKey\"];\nif (string.IsNullOrEmpty(key))\n    throw new InvalidOperationException(\"AzureSubKey missing from configuration\");","typeGuard":"bool HasSubscriptionKey(string key) => !string.IsNullOrWhiteSpace(key);","tryCatchPattern":"try\n{\n    var auth = new AzureAuthToken(key, tokenValue);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"key\")\n{\n    logger.LogError(\"Subscription key not configured: {Message}\", ex.Message);\n    throw new InvalidOperationException(\"Configure the Cognitive Services subscription key\", ex);\n}","preventionTips":["Load keys from a strongly-typed configuration class validated at startup","Null-check config values before constructing service clients","Never pass GetSection(...).Value directly without a presence check","Document required config keys and check them with a startup validator"],"tags":["csharp","azure","configuration","constructor"],"backgroundTag":"null-argument","analyzedSha":"89fe2fc62081145ec5408d42059cbcb7c4a033c8","analyzedAt":"2026-09-13T20:10:53.835Z","contentChangedAt":"2026-09-13T20:10:53.835Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}