c# - http add sslcert fails when done programatically -


i have developed self-hosted api.

the api traffic needs run on ssl.

using combination of netsh commands have managed add certificate , bind route service. happy days.

but, have write installer programmatically.

the problem when add certificate using c# code, can see certificate mmc when try bind error of:

ssl certificate add failed, error: 1312 specified log-on session not exist. may have been terminated. 

as say, when manually these steps don't problem...

  1. list item
  2. double click on .pfx file.
  3. mmc opens.
  4. i select "local machine"
  5. on next screen confirm .pfx file location , name.
  6. i enter password certificate , select "include extended properties"
  7. on next screen let default "automatically select certificate store based on type of certificate"
  8. i confirmation screen.
  9. when click "finish" message "the import successful"

i can see in mmc under personal > certificates

and lets me add route using netsh command prompt - happy days.

when try programmatically following code:

public static bool configuresslcertificate(string file, string password, string method)     {         try         {             x509certificate2 cert = new x509certificate2(file, password);              var store = new x509store(storename.my, storelocation.localmachine);             store.open(openflags.readwrite);             if (!store.certificates.contains(cert))             {                 if (method == "add")                 {                     store.add(cert);                 }             }             if (method == "remove")             {                 store.remove(cert);             }             return true;         }         catch { return false; }     } 

the certificate appears in mmc in same place when try , add route exact same netsh command before error mentioned above:

netsh>http add sslcert ipport=0.0.0.0:8088 certhash=fb93ce2c4d8bd88c82e63e3372a050ba84f15e94 appid={bb14356a-a14f-4589-82ce-b80d38b8741e} 

for reason, when add certificate manually using mmc , when run code different. stopping route being added.

can suggest doing wrong please?

the solution simple - have struggled this, , have found solution. how can manually added certificate differ programatically added one? well, short answer change certificate load line this:

x509certificate2 cert = new x509certificate2(file, password, x509keystorageflags.machinekeyset); 

the key being last parameter, tells certificate save private key stored in machine location, , not user location. netsh command can find private key, , can work.

the solution found in explanatory text paul stovell , digging see how set flag when loading certificate store.

now, why can't programmatically netsh function matter...


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -