Create user key in "Microsoft Software Key Storage Provider" via NCrypt from a background process

Martin A 1 Reputation point
2021-11-03T07:40:20.3+00:00

Background
I have an application running as a background process on Windows 10 as a different user account (userX) than the currently logged on user (userA). This application needs to be able to create/open RSA keys in the "Microsoft Software Key Storage Provider" via NCrypt.

Problem
I have tried to create a user key in the application. This fails in the call to NCryptCreatePersistedKey with the error code 0x80070002. If I run the application as userA everything works as expected. If I log on to the computer as userX, select "Switch user" in Windows, log on as userA and run the application as userX, everything works as expected. If I open a command prompt and run runas /user:userX cmd, everything works as expected while the prompt is open. So, it seems like I am not able to create a user key unless the user is interactively logged on to the computer. Is this correct?

Is there a way to create user keys via NCrypt API without being interactively logged on to the computer?

----
I would like to add more related tags such as ncrypt, cng etc. but I am forced to select from the predefined tags.

Windows for business Windows Client for IT Pros Devices and deployment Configure application groups
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.