मैंने यह बहुत कुछ किया है। यहां कुछ आसान कोड है जिसका आप उपयोग कर सकते हैं।
if (!isChainValid)
ब्लॉक में हिस्सा एक सुंदर त्रुटि संदेश बनाना है। यदि आप नहीं चाहते हैं तो आपको इसका उपयोग करने की ज़रूरत नहीं है, लेकिन यदि श्रृंखला नहीं बनाई जा सकती है तो आपको एक त्रुटि फेंकनी चाहिए। आपकी जड़ की जांच करने के लिए श्रृंखला तत्व आवश्यक हैं।
X509Certificate2 authority = GetAuthorityCertificate();
X509Certificate2 certificateToValidate = GetCertificateToValidate();
X509Chain chain = new X509Chain();
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
chain.ChainPolicy.VerificationTime = DateTime.Now;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 0, 0);
// This part is very important. You're adding your known root here.
// It doesn't have to be in the computer store at all. Neither certificates do.
chain.ChainPolicy.ExtraStore.Add(authority);
bool isChainValid = chain.Build(certificateToValidate);
if (!isChainValid)
{
string[] errors = chain.ChainStatus
.Select(x => String.Format("{0} ({1})", x.StatusInformation.Trim(), x.Status))
.ToArray();
string certificateErrorsString = "Unknown errors.";
if (errors != null && errors.Length > 0)
{
certificateErrorsString = String.Join(", ", errors);
}
throw new Exception("Trust chain did not complete to the known authority anchor. Errors: " + certificateErrorsString);
}
// This piece makes sure it actually matches your known root
var valid = chain.ChainElements
.Cast<X509ChainElement>()
.Any(x => x.Certificate.Thumbprint == authority.Thumbprint);
if (!valid)
{
throw new Exception("Trust chain did not complete to the known authority anchor. Thumbprints did not match.");
}
स्रोत
2013-06-20 23:30:07
security.se (शायद) – Woot4Moo
क्यों होना चाहिए? मेरा सवाल यह है कि कोड में साइन इन को कैसे सत्यापित किया जाए ... –
क्या आपको यह कोड करने का कोई तरीका मिला? – MiguelM