dotnet/wpf · error · FileFormatException

SR.InvalidTypePrefixedUserName

Error message

SR.InvalidTypePrefixedUserName

What it means

ParseTypePrefixedUserName splits strings like "windows:DOMAIN\\alias" on the first ':'. It throws FileFormatException with SR.InvalidTypePrefixedUserName when there is no colon, or nothing before/after it, so the string is not a valid type-prefixed user name stored in the use-license stream.

Solutions

  1. Rebuild the user name string as "windows:DOMAIN\\alias" or "passport:user@hotmail.com" format
  2. Regenerate the protected document to restore a valid stored user name
  3. Validate the type-prefixed format (non-empty prefix and name around a colon) before persisting
  4. Catch FileFormatException and treat the document's use license as invalid

Example fix

// before
string userName = "DOMAIN\\alias"; // missing type prefix
// after
string userName = "windows:DOMAIN\\alias";
Defensive patterns

Strategy: try-catch

Validate before calling

static bool IsTypePrefixedUserName(string s) { int i = s?.IndexOf(':') ?? -1; return i > 0 && i < s.Length - 1; }

Type guard

bool IsValidTypePrefixedUserName(string s) { if (string.IsNullOrEmpty(s)) return false; int i = s.IndexOf(':'); return i > 0 && i < s.Length - 1; }

Try / catch

try { ParseTypePrefixedUserName(raw); } catch (FileFormatException) { /* invalid stored user name: regenerate or ignore entry */ }

Prevention

When it happens

Trigger: Loading a use license whose embedded user name lacks the 'type:' prefix, has an empty type, or an empty user name (colon at index 0 or at the end of the string).

Common situations: Corrupted or hand-crafted protected documents; strings built without the prefix (plain 'user@domain.com'); stream data written by a buggy or non-WPF RM writer.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/c3369a58275ad10e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/RightsManagementEncryptionTransform.cs:1058

        /// <param name="typePrefixedUserName">
        /// The string to be parsed.
        /// </param>
        /// <param name="authenticationType">
        /// Specifies whether the string represents a Windows or Passport user ID.
        /// </param>
        /// <param name="userName">
        /// The user's ID.
        /// </param>
        private static void ParseTypePrefixedUserName(string typePrefixedUserName, out AuthenticationType authenticationType, out string userName)
        {
            // We don't actually know the authentication type yet, and we might find that
            // the type-prefixed user name doesn't even specify a valid authentication
            // type. But we have to assign to authenticationType because it's an out
            // parameter.
            int colonIndex = typePrefixedUserName.IndexOf(':');
            if (colonIndex < 1 || colonIndex >= typePrefixedUserName.Length - 1)
            {
                throw new FileFormatException(SR.InvalidTypePrefixedUserName);
            }

            // No need to use checked{} here since colonIndex cannot be >= to (max int - 1)
            userName = typePrefixedUserName.Substring(colonIndex + 1);

            // Usernames: Case-Insensitive comparison
            ReadOnlySpan<char> authenticationSpan = typePrefixedUserName.AsSpan(0, colonIndex);

            if (authenticationSpan.Equals(nameof(AuthenticationType.Windows), StringComparison.OrdinalIgnoreCase))
                authenticationType = AuthenticationType.Windows;
            else if (authenticationSpan.Equals(nameof(AuthenticationType.Passport), StringComparison.OrdinalIgnoreCase))
                authenticationType = AuthenticationType.Passport;
            else // Didn't find a matching enumeration constant.
                throw new FileFormatException(SR.Format(SR.InvalidAuthenticationTypeString, typePrefixedUserName));
        }

        /// <summary>
        /// Load the use license from the specified stream.

View on GitHub (pinned to 81131a70a4)