dotnet/Documentation/compatibility/cspparameters_parentwindowh...

2.3 KiB
Raw Blame History

CspParameters.ParentWindowHandle now expects HWND value

Scope

Minor

Version Introduced

4.7

Source Analyzer Status

Investigating

Change Description

The CspParameters.ParentWindowHandle value, introduced in .NET Framework 2.0, allows an application to register a parent window handle value such that any UI required to access the key (such as a PIN prompt or consent dialog) opens as a modal child to the specified window.

Starting with apps that target the .NET Framework 4.7, a Windows Forms application can set the CspParameters.ParentWindowHandle property with code like the following:

cspParameters.ParentWindowHandle = form.Handle;

In previous versions of the .NET Framework, the value was expected to be an IntPtr representing a location in memory where the HWND value resided. Setting the property to form.Handle on Windows 7 and earlier versions had no effect, but on Windows 8 and later versions, it results in a <20>CryptographicException: The parameter is incorrect.<2E>

  • Quirked
  • Build-time break

Applications targeting .NET 4.7 or higher wishing to register a parent window relationship are encouraged to use the simplified form:

cspParameters.ParentWindowHandle = form.Handle;

Users who had identified that the correct value to pass was the address of a memory location which held the value form.Handle can opt out of the behavior change by setting the AppContext switch Switch.System.Security.Cryptography.DoNotAddrOfCspParentWindowHandle to true.

  1. By programmatically setting compat switches on the AppContext, as explained here
  2. By adding the following line to the <runtime> section of the app.config file:
<runtime>
  <AppContextSwitchOverrides value="Switch.System.Security.Cryptography.DoNotAddrOfCspParentWindowHandle=true"/>
</runtime>

Conversely, users who wish to opt in to the new behavior on the .NET Framework 4.7 runtime when the application loads under older .NET Framework versions can set the AppContext switch to false.

Affected APIs

  • P:System.Security.Cryptography.CspParameters.ParentWindowHandle

Category

Security