c# - Impersonating to X509Store.Add to StoreName.Root - The Request Is Not Supported -


i cannot find related error encountering.

the problem encountering cryptographicexception on store.add below:

"the request not supported."

the msdn documentation not helpful, states that:

the certificate not added store.

i used following code guide, following change:

  • they adding storename.my , i want add storename.root
  • i want use idisposable class impersonate user.

my proof of concept code (abridged/shoddy):

public class userlogonimpersonator : idisposable {     private windowsimpersonationcontext _impersonationcontext = null;     private const int logon_interactive = 2;     private const int provider_default = 0;      [dllimport("advapi32.dll", setlasterror=true)]         private static extern int logonuser(         string lpszusername,         string lpszdomain,         string lpszpassword,         int dwlogontype,         int dwlogonprovider,         ref intptr phtoken);      public userlogonimpersonator()     {         _impersonationcontext = null;         intptr userhandle = intptr.zero;         const string domain = "domain";         const string user = "user";         const string hcpw = "password";          try         {             int logonresult = logonuser(user, domain, hcpw,                  logon_interactive, provider_default, ref userhandle);             bool isloggedon = logonresult != 0;              if (isloggedon)             {                 _impersonationcontext =                      windowsidentity.impersonate(userhandle);             }          }         catch(exception e)         {             // handle exception         }     }      public void dispose()     {         // undoimpersonation();     }      // private methods ... }  public class certservice {     public void addcerttorootstore()     {         using(new userlogonimpersonator())         {             x509certificate2 rootcert =                  new x509certificate2(certdata.certfilepath);             x509store store =                  new x509store(storename.root, storelocation.currentuser);             store.open(openflags.maxallowed);             store.add(rootcert);             store.close();         }     } } 

i can remove impersonation , no exceptions thrown, not correct user's store.

with impersonation, can put cert storename.authroot without exception. not store want cert go into.

neither of these exception-free solutions work. require program run elevated privileges , go user's store.

i solved doing manually.

i wanted automate "test chain certificates." our third party ca gave set of certificates .local domain.

our real-life use case have root , chain certificate installed.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -