स्पष्ट रूप से मैं अपने पिछले पोस्ट में गलत सवाल पूछ रहा था। मेरे पास एक सुरक्षित वेब साइट (https://..) के रूप में चल रहे X.50 9 प्रमाण पत्र के साथ सुरक्षित एक वेब सेवा है। मैं कंपनी के रूट सीए द्वारा जारी किए गए क्लाइंट के मशीन सर्टिफिकेट (X.50 9) का उपयोग करना चाहता हूं ताकि सर्वर को सत्यापित किया जा सके कि क्लाइंट मशीन सेवा का उपयोग करने के लिए अधिकृत है। ऐसा करने के लिए, मुझे प्रमाण पत्र का निरीक्षण करने और कुछ पहचान करने वाली सुविधा की आवश्यकता है और डेटाबेस में संग्रहीत मूल्य (शायद थंबप्रिंट?) से मेल खाना चाहिए। , तबमैं वेब सेवा में क्लाइंट से भेजे गए X509 प्रमाणपत्र कैसे प्राप्त करूं?
public static class SecurityCertificate
{
private static X509Certificate2 _certificate = null;
public static X509Certificate2 Certificate
{
get { return _certificate; }
}
public static bool LoadCertificate()
{
// get thumbprint from app.config
string thumbPrint = Properties.Settings.Default.Thumbprint;
if (string.IsNullOrEmpty(thumbPrint))
{
// if no thumbprint on file, user must select certificate to use
_certificate = PickCertificate(StoreLocation.LocalMachine, StoreName.My);
if (null != _certificate)
{
// show certificate details dialog
X509Certificate2UI.DisplayCertificate(_certificate);
Properties.Settings.Default.Thumbprint = _certificate.Thumbprint;
Properties.Settings.Default.Save();
}
}
else
{
_certificate = FindCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, thumbPrint);
}
if (null == _certificate)
{
MessageBox.Show("You must have a valid machine certificate to use STS.");
return false;
}
return true;
}
private static X509Certificate2 PickCertificate(StoreLocation location, StoreName name)
{
X509Store store = new X509Store(name, location);
try
{
// create and open store for read-only access
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection coll = store.Certificates.Find(X509FindType.FindByIssuerName, STSClientConstants.NBCCA, true);
if (0 == coll.Count)
{
MessageBox.Show("No valid machine certificate found - please contact tech support.");
return null;
}
// pick a certificate from the store
coll = null;
while (null == coll || 0 == coll.Count)
{
coll = X509Certificate2UI.SelectFromCollection(
store.Certificates, "Local Machine Certificates",
"Select one", X509SelectionFlag.SingleSelection);
}
// return first certificate found
return coll[ 0 ];
}
// always close the store
finally { store.Close(); }
}
private static X509Certificate2 FindCertificate(StoreLocation location, StoreName name, X509FindType findType, string findValue)
{
X509Store store = new X509Store(name, location);
try
{
// create and open store for read-only access
store.Open(OpenFlags.ReadOnly);
// search store
X509Certificate2Collection col = store.Certificates.Find(findType, findValue, true);
// return first certificate found
return col[ 0 ];
}
// always close the store
finally { store.Close(); }
}
मैं thusly आउटबाउंड धारा को प्रमाण पत्र देते हैं:
यहाँ कोड मैं स्थानीय प्रमाणपत्र संग्रह से प्रमाण पत्र प्राप्त करने के लिए उपयोग है (http://msdn.microsoft.com/en-us/magazine/cc163454.aspx से सीधे उठा लिया)
public static class ServiceDataAccess
{
private static STSWebService _stsWebService = new STSWebService();
public static DataSet GetData(Dictionary<string,string> DictParam, string action)
{
// add the machine certificate here, the first web service call made by the program (only called once)
_stsWebService.ClientCertificates.Add(SecurityCertificate.Certificate);
// rest of web service call here...
}
}
मेरा सवाल यह है - मैं वेब सेवा कोड में प्रमाणपत्र कैसे प्राप्त करूं? अधिकांश नमूना कोड स्निपेट्स मैं उस कवर में आया हूं कि कस्टम सत्यापन कैसे करें, वहां GetCertificate() कॉल है, जाहिर है कि यह मानना इतना आसान है कि हर किसी को यह जानना चाहिए कि इसे कैसे करना है?
मेरा मुख्य वर्ग वेब सेवा से प्राप्त होता है, इसलिए मैं प्रमाण पत्र प्राप्त करने के लिए Context.Request.ClientCertificate का उपयोग कर सकता हूं, लेकिन यह एक HttpClientCertificate है, X509Certificate2 नहीं। HttpContext मुझे एक ही परिणाम देता है। सत्यापन के लिए एक कस्टम सी # विधि को कॉल करने के तरीके के बारे में कोई संकेत नहीं के साथ, अन्य दृष्टिकोण सभी वेब कॉन्फ़िगरेशन कोड का उपयोग पूर्व परिभाषित सत्यापन कोड को कॉल करने के लिए करते हैं।
मुझे यकीन है कि यह एक और "बेवकूफ सवाल" है कि कोई वापस आकर कहेंगे "देखो, बस यह करें और यह काम करता है," लेकिन यह ठीक है। मैंने इसे काम करने के लिए कई घंटे बिताए हैं, और इस बिंदु पर मेरा गौरव लगभग कोई नहीं है। क्या कोई मुझे मेरे तरीकों की त्रुटि दिखा सकता है?
धन्यवाद, डेव
यदि आपके पास HttpContext नहीं है, तो http: // stackoverflow के लिए उत्तर देखें।कॉम/प्रश्न/7528455/कैसे-से-get-the-x509certificate-from-a-client-request? lq = 1 –