本文整理汇总了C#中System.Security.Cryptography.X509Certificates.X509Store.?.Close方法的典型用法代码示例。如果您正苦于以下问题:C# X509Store.?.Close方法的具体用法?C# X509Store.?.Close怎么用?C# X509Store.?.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.X509Certificates.X509Store
的用法示例。
在下文中一共展示了X509Store.?.Close方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCertificate
/// <summary>
/// Retrieves an X509 Certificate from the specified store and location
/// </summary>
/// <param name="thumbprint">The certificate thumbprint</param>
/// <param name="storeName">The name of the store to retrieve the information from</param>
/// <param name="storeLocation">The location within the store where the certificate is located</param>
/// <returns>An X509 certificate with the specified thumbprint if available or null if not</returns>
public static X509Certificate2 GetCertificate(string thumbprint, StoreName storeName, StoreLocation storeLocation)
{
X509Store store = null;
X509Certificate2 certificate;
try
{
store = new X509Store(storeName, storeLocation);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
certificate = collection.Count == 0 ? null : collection[0];
}
finally
{
store?.Close();
}
return certificate;
}
示例2: GetCertificate
public static X509Certificate2 GetCertificate(StoreLocation location, string subjectName)
{
X509Certificate2 cert = null;
var store = new X509Store(StoreName.My, location);
store.Open(OpenFlags.ReadOnly);
try
{
X509Certificate2Collection certs = store.Certificates.Find(
X509FindType.FindBySubjectName,
subjectName,
false);
if (certs.Count == 1)
{
cert = certs[0];
}
else
{
cert = null;
}
}
finally
{
store?.Close();
}
return cert;
}
示例3: FindCertificates
/// <summary>
/// Find certificates matching some predicate.
/// </summary>
/// <param name="predicate">The function used to determine which certificates to include.</param>
/// <returns>The matching certificates.</returns>
public static IEnumerable<X509Certificate2> FindCertificates(Func<X509Certificate2, bool> predicate)
{
var stores = Enum.GetValues(typeof(StoreName)).Cast<StoreName>().ToArray();
var locations = Enum.GetValues(typeof(StoreLocation)).Cast<StoreLocation>().ToArray();
var certificates = new List<X509Certificate2>();
foreach (var store in stores)
{
foreach (var location in locations)
{
X509Store source = null;
try
{
source = new X509Store(store, location);
source.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
certificates.AddRange(source.Certificates
.Cast<X509Certificate2>()
.Where(x => !x.Archived)
.Where(x => x.NotAfter >= DateTime.Now)
.Where(x => x.NotBefore <= DateTime.Now)
.Where(predicate)
.Where(x => x.Verify())
);
}
catch
{
// Just try the next one
}
finally
{
source?.Close();
}
}
}
return certificates;
}