{"record":{"id":"308596fdbecc1f9f","repo":"microsoft/aspire","slug":"secret-name-can-only-contain-ascii-letters-a-z-a-z-digits-0","errorCode":null,"errorMessage":"Secret name can only contain ASCII letters (a-z, A-Z), digits (0-9), and dashes (-).","messagePattern":"Secret name can only contain ASCII letters \\(a-z, A-Z\\), digits \\(0-9\\), and dashes \\(-\\)\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.KeyVault/AzureKeyVaultResourceExtensions.cs","lineNumber":396,"sourceCode":"        ValidateSecretName(secretName);\n\n        var secret = new AzureKeyVaultSecretResource(name, secretName, builder.Resource, value);\n        builder.Resource.Secrets.Add(secret);\n\n        return builder.ApplicationBuilder.AddResource(secret).WithIconName(\"LockClosed\").ExcludeFromManifest();\n    }\n\n    private static void ValidateSecretName(string secretName)\n    {\n        // Azure Key Vault secret names must be 1-127 characters long and contain only ASCII letters (a-z, A-Z), digits (0-9), and dashes (-)\n        if (secretName.Length > 127)\n        {\n            throw new ArgumentException(\"Secret name cannot be longer than 127 characters.\", nameof(secretName));\n        }\n\n        if (!AzureKeyVaultSecretNameRegex().IsMatch(secretName))\n        {\n            throw new ArgumentException(\"Secret name can only contain ASCII letters (a-z, A-Z), digits (0-9), and dashes (-).\", nameof(secretName));\n        }\n    }\n\n    [GeneratedRegex(\"^[a-zA-Z0-9-]+$\")]\n    private static partial Regex AzureKeyVaultSecretNameRegex();\n}\n","sourceCodeStart":378,"sourceCodeEnd":403,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.KeyVault/AzureKeyVaultResourceExtensions.cs#L378-L403","documentation":"Azure Key Vault secret names may contain only ASCII letters (a-z, A-Z), digits (0-9), and dashes (-), enforced by the regex ^[a-zA-Z0-9-]+$. ValidateSecretName throws ArgumentException when the name contains any other character (underscores, dots, spaces, non-ASCII, or is empty).","triggerScenarios":"Calling AddSecret with a secret name containing invalid characters such as '_', '.', ':', '/', spaces, or non-ASCII letters — or an empty string (which fails the + quantifier).","commonSituations":"Deriving secret names from connection or setting names that use underscores (e.g. 'My_Connection'), from user input, or from resource names that include dots; classic Azure naming differences trip developers up.","solutions":["Replace invalid characters with dashes: secretName.Replace('_', '-').","Sanitize the name before calling AddSecret, e.g. Regex.Replace(name, \"[^a-zA-Z0-9-]\", \"-\").","Keep the secret name as an explicit short literal instead of deriving it from other identifiers."],"exampleFix":"// before\nkv.AddSecret(\"my_secret.name\", ...);\n\n// after\nvar safeName = Regex.Replace(\"my_secret.name\", \"[^a-zA-Z0-9-]\", \"-\"); // \"my-secret-name\"\nkv.AddSecret(safeName, ...);","handlingStrategy":"validation","validationCode":"var sanitized = Regex.Replace(secretName ?? \"\", \"[^a-zA-Z0-9-]\", \"-\");\nif (sanitized.Length == 0) throw new ArgumentException(\"Secret name cannot be empty after sanitization.\");","typeGuard":"bool IsValidSecretName(string n) => !string.IsNullOrEmpty(n) && Regex.IsMatch(n, \"^[a-zA-Z0-9-]+$\");","tryCatchPattern":"try { kv.AddSecret(secretName, ...); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"ASCII letters\")) { /* sanitize and retry */ }","preventionTips":["Sanitize identifiers (underscores, dots, spaces) to dashes before adding secrets.","Remember the regex allows only letters, digits, and dashes — no underscores.","Test generated names with the same regex used by the library."],"tags":["azure","key-vault","naming","regex"],"backgroundTag":"invalid-identifier-format","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}