예제 #1
0
파일: SecurityUtils.cs 프로젝트: yukozh/wcf
        private static X509Certificate2 GetCertificateFromStoreCore(StoreName storeName, StoreLocation storeLocation,
                                                                    X509FindType findType, object findValue, EndpointAddress target, bool throwIfMultipleOrNoMatch)
        {
            if (findValue == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("findValue");
            }

            X509Store store = new X509Store(storeName, storeLocation);
            X509Certificate2Collection certs = null;

            try
            {
                store.Open(OpenFlags.ReadOnly);
                certs = store.Certificates.Find(findType, findValue, false);
                if (certs.Count == 1)
                {
                    return(new X509Certificate2(certs[0].Handle));
                }
                if (throwIfMultipleOrNoMatch)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateCertificateLoadException(
                                                                                  storeName, storeLocation, findType, findValue, target, certs.Count));
                }
                else
                {
                    return(null);
                }
            }
            finally
            {
                SecurityUtils.ResetAllCertificates(certs);
                store.Dispose();
            }
        }