c# - Programatically adding certificate to personal store -
the project i'm working on consists of mvc website talking wcf web services, authenticated windows identity. have certificate identity delegation i'm trying add programatically. manually open certificates snap-in in mmc, import .pfx file personal , enter password. have click on manage private keys , allow permission iis_iusrs. replicate process i've come following console app:
class program { static void main(string[] args) { var cert = new x509certificate2("location.pfx", "password", x509keystorageflags.machinekeyset); addcert(storename.my, storelocation.localmachine, cert); addaccesstocertificate(cert, "iis_iusrs"); } private static void addcert(storename storename, storelocation storelocation, x509certificate2 cert) { x509store store = new x509store(storename, storelocation); store.open(openflags.readwrite); store.add(cert); store.close(); } private static void addaccesstocertificate(x509certificate2 cert, string user) { rsacryptoserviceprovider rsa = cert.privatekey rsacryptoserviceprovider; if (rsa != null) { string keyfilepath = findkeylocation(rsa.cspkeycontainerinfo.uniquekeycontainername); fileinfo file = new fileinfo(keyfilepath + "\\" + rsa.cspkeycontainerinfo.uniquekeycontainername); filesecurity fs = file.getaccesscontrol(); ntaccount account = new ntaccount(user); fs.addaccessrule(new filesystemaccessrule(account, filesystemrights.fullcontrol, accesscontroltype.allow)); file.setaccesscontrol(fs); } } private static string findkeylocation(string keyfilename) { string text1 = environment.getfolderpath(environment.specialfolder.commonapplicationdata); string text2 = text1 + @"\microsoft\crypto\rsa\machinekeys"; string[] textarray1 = directory.getfiles(text2, keyfilename); if (textarray1.length > 0) { return text2; } string text3 = environment.getfolderpath(environment.specialfolder.applicationdata); string text4 = text3 + @"\microsoft\crypto\rsa\"; textarray1 = directory.getdirectories(text4); if (textarray1.length > 0) { foreach (string text5 in textarray1) { textarray1 = directory.getfiles(text5, keyfilename); if (textarray1.length != 0) { return text5; } } } return "private key exists not accessible"; } }
unfortunately gives error:
the address of security token issuer not specified. explicit issuer address must specified in binding target 'https://service.svc' or local issuer address must configured in credentials.
i recognise have large knowledge gap stuff i'd appreciate guidance!
my question is, what's difference between manual , automated process?
this line:
var cert = new x509certificate2("location.pfx", "password", x509keystorageflags.machinekeyset);
should have been
var cert = new x509certificate2("location.pfx", "password", x509keystorageflags.machinekeyset | x509keystorageflags.persistkeyset);
it x509keystorageflags.persistkeyset
missing.
Comments
Post a Comment