{"record":{"id":"ada04589b3125e2c","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-useractivationmode","errorCode":null,"errorMessage":"ArgumentOutOfRangeException(userActivationMode)","messagePattern":"ArgumentOutOfRangeException\\(userActivationMode\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/SecureEnvironment.cs","lineNumber":281,"sourceCode":"        }\n\n        private static SecureEnvironment CriticalCreate(\n            string applicationManifest, \n            AuthenticationType authentication,\n            UserActivationMode userActivationMode)\n        {\n            ArgumentNullException.ThrowIfNull(applicationManifest);\n\n            if ((authentication != AuthenticationType.Windows) && \n                 (authentication != AuthenticationType.Passport))\n            {\n                throw new ArgumentOutOfRangeException(nameof(authentication));\n            }\n\n            if ((userActivationMode != UserActivationMode.Permanent) &&\n                 (userActivationMode != UserActivationMode.Temporary))\n            {\n                throw new ArgumentOutOfRangeException(nameof(userActivationMode));            \n            }\n\n            //build user with the given authnetication type and a default name \n            // only authentication type is critical in this case \n            ContentUser user; \n            \n            using (ClientSession tempClientSession =\n                ClientSession.DefaultUserClientSession(authentication))\n            {\n                //Activate Machine if neccessary\n                if (!tempClientSession.IsMachineActivated())\n                {\n                    // activate Machine\n                    tempClientSession.ActivateMachine(authentication);\n                }\n\n                //Activate User (we will force start activation at this point)\n                // at this point we should have a real user name ","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/SecureEnvironment.cs#L263-L299","documentation":"SecureEnvironment.Create(applicationManifest, AuthenticationType, UserActivationMode) accepts only UserActivationMode.Permanent or UserActivationMode.Temporary. Any other UserActivationMode value throws ArgumentOutOfRangeException for 'userActivationMode', guarding against undefined activation semantics.","triggerScenarios":"Calling the activation overload of SecureEnvironment.Create with a UserActivationMode value that is not Permanent or Temporary — typically an uninitialized enum (0), a cast from an int, or a value from configuration.","commonSituations":"Defaulting an enum field that never got set (default(UserActivationMode) or 0 is not a defined mode); deserializing an int from config; a settings binding failing and leaving the enum at its default.","solutions":["Pass UserActivationMode.Permanent explicitly (persist activation across runs) or UserActivationMode.Temporary (destroyed on Dispose)","Check any enum value read from config/settings for defined values before calling Create","Initialize enum fields with a valid value rather than relying on the 0 default"],"exampleFix":"// before\nUserActivationMode mode = default; // 0, not defined\nvar env = SecureEnvironment.Create(manifest, AuthenticationType.Windows, mode);\n\n// after\nUserActivationMode mode = UserActivationMode.Permanent;\nvar env = SecureEnvironment.Create(manifest, AuthenticationType.Windows, mode);","handlingStrategy":"validation","validationCode":"if (userActivationMode is not (UserActivationMode.Permanent or UserActivationMode.Temporary))\n    throw new InvalidOperationException($\"Invalid UserActivationMode: {userActivationMode}\");","typeGuard":"static bool IsValidActivationMode(UserActivationMode m) =>\n    m is UserActivationMode.Permanent or UserActivationMode.Temporary;","tryCatchPattern":"try { var env = SecureEnvironment.Create(manifest, auth, userActivationMode); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"userActivationMode\")\n{\n    logger.LogError(ex, \"Activation mode {Mode} undefined\", userActivationMode);\n}","preventionTips":["Initialize enum settings with explicit valid values, never default(0)","When loading UserActivationMode from config, validate against Enum.IsDefined before use","Prefer explicit fields: mode = UserActivationMode.Permanent"],"tags":["rights-management","argument-validation","enum","wpf"],"backgroundTag":"invalid-enum-value","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"}