c# - Private key is null when accessing via code, why? -
i have certificate installed on machine , when go view it, see message "you have private key corresponds certificate" however, when seek access private key in code, null. utilize next code certificate:
var x509certificate = getcertificate(storename.my, storelocation.localmachine, "cn=somecert");
where:
public x509certificate2 getcertificate(string storename, string storelocation, string subjectname) { var store = new x509store(getstorename(storename), getstorelocation(storelocation)); x509certificate2collection certificates = null; store.open(openflags.readonly); seek { x509certificate2 result = null; certificates = store.certificates; homecoming getcertificateresult(certificates, subjectname, result); } { if (certificates != null) { foreach (var cert in certificates) { cert.reset(); } } store.close(); } }
and:
private static x509certificate2 getcertificateresult(ienumerable certificates, string subjectname, x509certificate2 result) { foreach (var cert in certificates.cast<x509certificate2>().where(cert => cert.subjectname.name != null && cert.subjectname.name.tolower() == subjectname.tolower())) { if (result != null) { throw new applicationexception(string.format("there more 1 certificate found subject name {0}", subjectname)); } result = new x509certificate2(cert); } if (result == null) { throw new applicationexception(string.format("no certificate found subject name {0}", subjectname)); } homecoming result; }
i certificate fine, when seek access private key, doing following:
x509certificate.privatekey
the value privatekey null. doing wrong? need value sign saml2 request.
note: understand have abstractions in there point certificate (it's found) private key null. if there more info abstraction preventing question beingness answered, can provide more detail.
as it's described here .cer
file (i guess it's applicable certificate formats) can't contain private key. , looks correctly security point of view because file public. x509certificate2
not certificate, it's container certificate , other stuff. that's why has property privatekey
. in case if need info in code , if have private key file (.pvk
) , password - can utilize .pfx
file instead of .cer
. can created using pvk2pfx
utility:
> makecert -r -pe -ss samplestorename -n "cn=sample" sample.cer -sky exchange -sv sample.pvk > pvk2pfx -pvk sample.pvk -pi samplepassword -spc sample.cer -pfx sample.pfx -f
c# asp.net certificate x509certificate x509certificate2
No comments:
Post a Comment