@@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.DataProtection.XmlEncryption;
1717internal static unsafe class XmlEncryptionExtensions
1818{
1919 // Used for testing edge case assembly loading errors
20- internal static Func < string , Type > _getType = name => Type . GetType ( name , throwOnError : false ) ;
20+ internal static Func < string , Type ? > _getType = name => Type . GetType ( name , throwOnError : false ) ;
2121
2222 public static XElement DecryptElement ( this XElement element , IActivator activator )
2323 {
@@ -73,10 +73,10 @@ public static XElement DecryptElement(this XElement element, IActivator activato
7373 [ UnconditionalSuppressMessage ( "Trimmer" , "IL2057" , Justification = "Type.GetType result is only useful with types that are referenced by DataProtection assembly." ) ]
7474 private static IXmlDecryptor CreateDecryptor ( IActivator activator , string decryptorTypeName )
7575 {
76- var resolvedTypeName = TypeForwardingActivator . TryForwardTypeName ( decryptorTypeName , out var forwardedTypeName )
77- ? forwardedTypeName
78- : decryptorTypeName ;
79- var type = _getType ( resolvedTypeName ) ;
76+ if ( ! TryGetDecryptorType ( decryptorTypeName , out var type ) )
77+ {
78+ return activator . CreateInstance < IXmlDecryptor > ( decryptorTypeName ) ;
79+ }
8080
8181 if ( type == typeof ( DpapiNGXmlDecryptor ) )
8282 {
@@ -98,6 +98,23 @@ private static IXmlDecryptor CreateDecryptor(IActivator activator, string decryp
9898 return activator . CreateInstance < IXmlDecryptor > ( decryptorTypeName ) ;
9999 }
100100
101+ private static bool TryGetDecryptorType ( string decryptorTypeName , [ NotNullWhen ( true ) ] out Type ? type )
102+ {
103+ var resolvedTypeName = TypeForwardingActivator . TryForwardTypeName ( decryptorTypeName , out var forwardedTypeName )
104+ ? forwardedTypeName
105+ : decryptorTypeName ;
106+ try
107+ {
108+ type = _getType ( resolvedTypeName ) ;
109+ return type is not null ;
110+ }
111+ catch
112+ {
113+ type = default ;
114+ return false ;
115+ }
116+ }
117+
101118 public static XElement ? EncryptIfNecessary ( this IXmlEncryptor encryptor , XElement element )
102119 {
103120 // If no encryption is necessary, return null.
0 commit comments