{"record":{"id":"2912f1df6c0bfd62","repo":"jstedfast/MailKit","slug":"username","errorCode":null,"errorMessage":"userName","messagePattern":"userName","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/Ntlm/NtlmAuthenticateMessage.cs","lineNumber":51,"sourceCode":"namespace MailKit.Security.Ntlm {\n\tclass NtlmAuthenticateMessage : NtlmMessageBase\n\t{\n\t\tstatic readonly byte[] Z16 = new byte[16];\n\n\t\treadonly NtlmNegotiateMessage? negotiate;\n\t\treadonly NtlmChallengeMessage? challenge;\n\t\tbyte[] clientChallenge;\n\n\t\tpublic NtlmAuthenticateMessage (NtlmNegotiateMessage negotiate, NtlmChallengeMessage challenge, string userName, string password, string domain, string workstation) : base (3)\n\t\t{\n\t\t\tif (negotiate == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (negotiate));\n\n\t\t\tif (challenge == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (challenge));\n\n\t\t\tif (userName == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (userName));\n\n\t\t\tif (password == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (password));\n\n\t\t\tclientChallenge = NtlmUtils.NONCE (8);\n\t\t\tthis.negotiate = negotiate;\n\t\t\tthis.challenge = challenge;\n\n\t\t\tif (!string.IsNullOrEmpty (domain)) {\n\t\t\t\tDomain = domain;\n\t\t\t} else if ((challenge.Flags & NtlmFlags.TargetTypeDomain) != 0) {\n\t\t\t\t// The server is domain-joined, so the TargetName will be the domain.\n\t\t\t\tDomain = challenge.TargetName ?? string.Empty;\n\t\t\t} else if (challenge.TargetInfo != null) {\n\t\t\t\t// The server is not domain-joined, so the TargetName will be the machine name of the server.\n\t\t\t\tDomain = challenge.TargetInfo.DomainName ?? string.Empty;\n\t\t\t} else {\n\t\t\t\tDomain = string.Empty;","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/Ntlm/NtlmAuthenticateMessage.cs#L33-L69","documentation":"The NtlmAuthenticateMessage constructor throws ArgumentNullException when userName is null. The username is embedded in the Type3 AUTHENTICATE message, so it must be provided (an empty string is acceptable, null is not).","triggerScenarios":"Calling new NtlmAuthenticateMessage(negotiate, challenge, null, password, domain, workstation), usually from unset credential variables or config fields.","commonSituations":"Credentials read from configuration/environment that were never populated; anonymous-auth code paths passing null instead of string.Empty.","solutions":["Pass a non-null userName (use string.Empty for anonymous/blank usernames).","Validate credentials are populated before starting the NTLM handshake.","Fix the config/env source so the username is loaded (e.g. check for missing settings)."],"exampleFix":"// before\nvar auth = new NtlmAuthenticateMessage(negotiate, challenge, null, pass, domain, ws);\n// after\nvar auth = new NtlmAuthenticateMessage(negotiate, challenge, userName ?? string.Empty, pass, domain, ws);","handlingStrategy":"validation","validationCode":"if (userName == null) throw new InvalidOperationException(\"NTLM username is not configured.\");\nvar auth = new NtlmAuthenticateMessage(negotiate, challenge, userName, password, domain, workstation);","typeGuard":"static bool HasUserName(string userName) => !string.IsNullOrEmpty(userName);","tryCatchPattern":"try {\n\tauth = new NtlmAuthenticateMessage(negotiate, challenge, userName, password, domain, workstation);\n} catch (ArgumentNullException ex) when (ex.ParamName == \"userName\") {\n\t// prompt for or reload credentials\n\tuserName = LoadConfiguredUserName() ?? string.Empty;\n}","preventionTips":["Load and validate credentials at startup, before any network work.","Use string.Empty (not null) for intentionally blank usernames.","Surface missing-credential config errors with clear messages."],"tags":["null-argument","csharp","ntlm","credentials"],"backgroundTag":"null-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}