{"record":{"id":"87bd8c1b7effff5e","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-name","errorCode":null,"errorMessage":"ArgumentOutOfRangeException(name)","messagePattern":"ArgumentOutOfRangeException\\(name\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/User.cs","lineNumber":26,"sourceCode":"{\n    /// <summary>\n    ///  This class represents a User for purposes of granting rights to that user, initializing secure environment for the user, \n    /// or enumerating rights granted to various users. \n    /// </summary>\n    public class ContentUser\n    {\n        /// <summary>\n        ///  This constructor creates a user that will be granted a right. Or used in other related scenarios like\n        /// initializing secure environment for the user, or enumerating rights granted to various users.         \n        /// </summary>\n        public ContentUser(string name, AuthenticationType authenticationType)\n        {\n\n            ArgumentNullException.ThrowIfNull(name);\n\n            if (name.Trim().Length == 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(name));\n            }\n\n            if ((authenticationType != AuthenticationType.Windows) &&\n                (authenticationType != AuthenticationType.Passport) &&\n                (authenticationType != AuthenticationType.WindowsPassport) &&\n                (authenticationType != AuthenticationType.Internal))\n            {\n                throw new ArgumentOutOfRangeException(nameof(authenticationType));\n            }\n\n            // We only support Anyone for the internal mode at the moment\n            if (authenticationType == AuthenticationType.Internal)\n            {\n                if (!CompareToAnyone(name) && !CompareToOwner(name))\n                {\n                    // we only support Anyone as internal user \n                    throw new ArgumentOutOfRangeException(nameof(name));\n                }","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/User.cs#L8-L44","documentation":"The ContentUser constructor rejects a name that is null or whose Trim() is empty, throwing ArgumentOutOfRangeException for 'name'. A user identity must be a non-blank string (e.g. DOMAIN\\user, an email, or 'Anyone'/'Owner' for the Internal auth type).","triggerScenarios":"Calling new ContentUser(name, authType) where name is \"\" or \"   \" — e.g. an empty username from Environment.UserName on a service account context, an unset config value, or a blank email field.","commonSituations":"Running under a context where the user name cannot be resolved (service sessions, certain CI environments); empty settings field for the licensed user; trimming input from a form and passing it without a check.","solutions":["Validate/trim the user name and fail fast with a clear message before constructing ContentUser","Resolve the identity robustly (e.g. WindowsIdentity.GetCurrent().Name) instead of Environment.UserName when it can be empty","For publishing, use the literal \"Anyone\" with AuthenticationType.Internal when an explicit identity is not required"],"exampleFix":"// before\nvar user = new ContentUser(settings.UserName, AuthenticationType.Windows);\n\n// after\nstring name = settings.UserName?.Trim();\nif (string.IsNullOrEmpty(name))\n    name = WindowsIdentity.GetCurrent().Name;\nvar user = new ContentUser(name, AuthenticationType.Windows);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(name))\n    throw new ArgumentException(\"User name must be a non-empty string\", nameof(name));","typeGuard":"static bool IsValidUserName(string name) => !string.IsNullOrWhiteSpace(name);","tryCatchPattern":"try { var user = new ContentUser(name, authType); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"name\")\n{\n    logger.LogError(ex, \"Blank user name supplied for ContentUser\");\n    throw new ApplicationException(\"A valid user name is required\");\n}","preventionTips":["Trim and validate identity strings at input boundaries (forms, config) before constructing ContentUser","Resolve the current identity with WindowsIdentity.GetCurrent().Name when Environment.UserName may be empty (services, CI)","For anonymous publishing use the literal \"Anyone\" with AuthenticationType.Internal rather than an empty name"],"tags":["rights-management","argument-validation","wpf"],"backgroundTag":"empty-required-field","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}