|
| 1 | +--- |
| 2 | +title: "Breaking change: X500DistinguishedName validation is stricter" |
| 3 | +description: Learn about the .NET 10 breaking change in core .NET libraries where X500DistinguishedName validation is stricter. |
| 4 | +ms.date: 01/30/2025 |
| 5 | +--- |
| 6 | +# X500DistinguishedName validation is stricter |
| 7 | + |
| 8 | +Starting in .NET 10, the <xref:System.Security.Cryptography.X509Certificates.X500DistinguishedName.%23ctor*> constructor that accepts a string-encoded distinguished name may reject previously accepted invalid input or encode it differently on non-Windows systems. This aligns with encoding specifications and Windows behavior. |
| 9 | + |
| 10 | +## Previous behavior |
| 11 | + |
| 12 | +Previous versions of .NET on non-Windows systems would permit incorrect distinguished names or encode them in a way not permitted by X.520 encoding rules. The <xref:System.Security.Cryptography.X509Certificates.X500DistinguishedNameFlags.ForceUTF8Encoding?displayProperty=nameWithType> flag would force components to use a UTF8String even if it was not a valid representation. |
| 13 | + |
| 14 | +## New behavior |
| 15 | + |
| 16 | +Starting in .NET 10, components violating encoding rules will throw a `CryptographicException` on non-Windows systems, matching Windows behavior. The <xref:System.Security.Cryptography.X509Certificates.X500DistinguishedNameFlags.ForceUTF8Encoding?displayProperty=nameWithType> flag will only UTF-8 encode components when permissible. |
| 17 | + |
| 18 | +## Version introduced |
| 19 | + |
| 20 | +.NET 10 Preview 1 |
| 21 | + |
| 22 | +## Type of breaking change |
| 23 | + |
| 24 | +This change is a [behavioral change](../../categories.md#behavioral-change). |
| 25 | + |
| 26 | +## Reason for change |
| 27 | + |
| 28 | +Different X.500 components have specific encoding rules. For example, `id-at-telephoneNumber` must be encoded as an ASN.1 <xref:System.Formats.Asn1.UniversalTagNumber.PrintableString>. The exclamation point character is invalid for a PrintableString, so: |
| 29 | + |
| 30 | +```C# |
| 31 | +new X500DistinguishedName("Phone=!!"); |
| 32 | +``` |
| 33 | + |
| 34 | +This would throw an exception on Windows but was encoded as a UTF8String on non-Windows. Similarly, using <xref:System.Security.Cryptography.X509Certificates.X500DistinguishedNameFlags.ForceUTF8Encoding?displayProperty=nameWithType> would force UTF8String encoding even when not permitted: |
| 35 | + |
| 36 | +```C# |
| 37 | +new X500DistinguishedName("Phone=000-555-1234", X500DistinguishedNameFlags.ForceUTF8Encoding); |
| 38 | +``` |
| 39 | + |
| 40 | +This change ensures encoding aligns with specifications and Windows behavior. |
| 41 | + |
| 42 | +## Recommended action |
| 43 | + |
| 44 | +Generally, no action is needed unless compatibility with incorrect encoding is required. Use <xref:System.Security.Cryptography.X509Certificates.X500DistinguishedNameBuilder?displayProperty=nameWithType> to create instances with desired encoding: |
| 45 | + |
| 46 | +```C# |
| 47 | +using System.Formats.Asn1; |
| 48 | +using System.Security.Cryptography.X509Certificates; |
| 49 | + |
| 50 | +X500DistinguishedNameBuilder builder = new(); |
| 51 | +builder.Add("2.5.4.20", "000-555-1234", UniversalTagNumber.UTF8String); |
| 52 | +X500DistinguishedName dn = builder.Build(); |
| 53 | +``` |
| 54 | + |
| 55 | +## Affected APIs |
| 56 | + |
| 57 | +- <xref:System.Security.Cryptography.X509Certificates.X500DistinguishedName.%23ctor(System.String)> |
| 58 | +- <xref:System.Security.Cryptography.X509Certificates.X500DistinguishedName.%23ctor(System.String,System.Security.Cryptography.X509Certificates.X500DistinguishedNameFlags)> |
| 59 | + |
0 commit comments