标题:X509Store 类

地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.x509certificates.x509store?view=netframework-4.8

表示 X.509 存储区,该存储区是保留和管理证书的物理存储区。 此类不能被继承。

示例

本部分包含两个示例。 第一个示例演示如何打开标准 x.509 存储, 并列出每个存储中的证书数量。

第二个示例演示如何添加和删除单个证书和证书范围。

示例1

此示例尝试打开当前计算机上每个标准位置中的每个标准存储区。 它将打印摘要, 其中显示每个存储是否存在, 如果存在, 则显示它包含的证书的数量。

该示例为标准X509Store名称和标准位置的每个组合创建一个对象。 Open 它OpenFlags.OpenExistingOnly通过标志调用方法, 该标志仅在已存在时打开物理存储区。 如果物理存储存在, 则该示例使用Name、 LocationCertificates属性显示存储区中的证书数量。

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates; public class Example
{
static void Main()
{
Console.WriteLine("\r\nExists Certs Name and Location");
Console.WriteLine("------ ----- -------------------------"); foreach (StoreLocation storeLocation in (StoreLocation[])
Enum.GetValues(typeof(StoreLocation)))
{
foreach (StoreName storeName in (StoreName[])
Enum.GetValues(typeof(StoreName)))
{
X509Store store = new X509Store(storeName, storeLocation); try
{
store.Open(OpenFlags.OpenExistingOnly); Console.WriteLine("Yes {0,4} {1}, {2}",
store.Certificates.Count, store.Name, store.Location);
}
catch (CryptographicException)
{
Console.WriteLine("No {0}, {1}",
store.Name, store.Location);
}
}
Console.WriteLine();
}
}
} /* This example produces output similar to the following: Exists Certs Name and Location
------ ----- -------------------------
Yes 1 AddressBook, CurrentUser
Yes 25 AuthRoot, CurrentUser
Yes 136 CA, CurrentUser
Yes 55 Disallowed, CurrentUser
Yes 20 My, CurrentUser
Yes 36 Root, CurrentUser
Yes 0 TrustedPeople, CurrentUser
Yes 1 TrustedPublisher, CurrentUser No AddressBook, LocalMachine
Yes 25 AuthRoot, LocalMachine
Yes 131 CA, LocalMachine
Yes 55 Disallowed, LocalMachine
Yes 3 My, LocalMachine
Yes 36 Root, LocalMachine
Yes 0 TrustedPeople, LocalMachine
Yes 1 TrustedPublisher, LocalMachine */

示例2

此示例将打开一个 x.509 证书存储区, 添加并删除证书, 然后关闭存储区。 它假定你有三个要在本地存储区中添加和删除的证书。

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.IO; public class X509store2
{
public static void Main (string[] args)
{
//Create new X509 store called teststore from the local certificate store.
X509Store store = new X509Store ("teststore", StoreLocation.CurrentUser);
store.Open (OpenFlags.ReadWrite);
X509Certificate2 certificate = new X509Certificate2 (); //Create certificates from certificate files.
//You must put in a valid path to three certificates in the following constructors.
X509Certificate2 certificate1 = new X509Certificate2 ("c:\\mycerts\\*****.cer");
X509Certificate2 certificate2 = new X509Certificate2 ("c:\\mycerts\\*****.cer");
X509Certificate2 certificate5 = new X509Certificate2 ("c:\\mycerts\\*****.cer"); //Create a collection and add two of the certificates.
X509Certificate2Collection collection = new X509Certificate2Collection ();
collection.Add (certificate2);
collection.Add (certificate5); //Add certificates to the store.
store.Add (certificate1);
store.AddRange (collection); X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
Console.WriteLine ("Store name: {0}", store.Name);
Console.WriteLine ("Store location: {0}", store.Location);
foreach (X509Certificate2 x509 in storecollection)
{
Console.WriteLine("certificate name: {0}",x509.Subject);
} //Remove a certificate.
store.Remove (certificate1);
X509Certificate2Collection storecollection2 = (X509Certificate2Collection)store.Certificates;
Console.WriteLine ("{1}Store name: {0}", store.Name, Environment.NewLine);
foreach (X509Certificate2 x509 in storecollection2)
{
Console.WriteLine ("certificate name: {0}", x509.Subject);
} //Remove a range of certificates.
store.RemoveRange (collection);
X509Certificate2Collection storecollection3 = (X509Certificate2Collection)store.Certificates;
Console.WriteLine ("{1}Store name: {0}", store.Name, Environment.NewLine);
if (storecollection3.Count == )
{
Console.WriteLine ("Store contains no certificates.");
}
else
{
foreach (X509Certificate2 x509 in storecollection3)
{
Console.WriteLine ("certificate name: {0}", x509.Subject);
}
} //Close the store.
store.Close ();
}
}

注解

使用此类处理 x.509 存储。

重要

从开始IDisposable , 此类型实现接口。 .NET Framework 4.6 在使用完类型后,您应直接或间接释放类型。 若要直接释放类型,请在 try/catch 块中调用其 Dispose 方法。 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。

对于面向.NET Framework 4.5.2和更早版本的应用X509Store , IDisposable类不实现接口, 因此不具有Dispose方法。

X509Store 类的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. 转: 在.NET中操作数字证书

    作者:玄魂出处:博客2010-06-23 12:05 http://winsystem.ctocio.com.cn/19/9492019.shtml .NET为我们提供了操作数字证书的两个主要的类,分 ...

  3. 用微软makecert.exe生成一个自签名的证书

    RT makecert.exe不用去找,安装VS2008后,在开始菜单相应的路径找到该命令提示符:Microsoft Visual Studio 2008/Visual Studio Tools/Vi ...

  4. 用makecert.exe生成一个自签名的证书

    makecert.exe不用去找,安装VS2008后,在开始菜单相应的路径找到该命令提示符:Microsoft Visual Studio 2008/Visual Studio Tools/Visua ...

  5. C#获取数字证书

    string Thumbprint = "C2489D912F247C187AA14B1291A6fB612281225D"; X509Store store = new X509 ...

  6. 使用makecert.exe创建数字证书

    RT makecert.exe不用去找,安装VS2008后,在开始菜单相应的路径找到该命令提示符:Microsoft Visual Studio 2008/Visual Studio Tools/Vi ...

  7. 【进阶修炼】——改善C#程序质量(7)

    113,声明变量时考虑最大值. Ushort的最大值是65535,用于不同的用途这个变量可能发生溢出,所以设计时应充分了解每个变量的最大值. 114,MD5不再安全. MD5多用于信息完整性的校验.R ...

  8. 在.NET中操作数字证书(新手教程)

    .NET为我们提供了操作数字证书的两个主要的类,分为为: System.Security.Cryptography.X509Certificates.X509Certificate2类, 每个这个类的 ...

  9. windows下如何制作和应用数字签名证书 全流程

    目前我们在发布应用程序时,有时用户下载后会被360杀毒当做木马直接隔离.为应用程序可执行文件打上数字签名可以让360杀毒放宽检测规则.下文是讲述如何制作数字签名证书的过程. 需要准备的工具:makec ...

随机推荐

  1. Linux下rpm及yum安装jdk

    笔者使用的Linux操作系统为CentOS7,Linux中有三种安装jdk的方式,在这里笔者主要介绍两种方式,即rpm和yum安装及卸载jdk,还有一种是直接从官网下载 tar.gz 压缩文件上传到L ...

  2. 【BigData】Java基础_定义工具类,对ArrayList排序并且求最大值、最小值、平均值

    需求描述 编写一个工具类,对ArrayList实现以下功能: ① 排序 ② 求最大值 ③ 求最小值 ④ 求平均值 需求实现 实现代码 package cn.test.logan.day04; impo ...

  3. Java 并发系列之九:java 原子操作类Atomic(13个)

    1. 原子更新基本类型类 2. 原子更新数组 3. 原子更新引用 4. 原子更新属性 5. txt java 原子操作类Atomic 概述 java.util.concurrent.atomic里的原 ...

  4. spring cloud 服务链路追踪 skywalking 6.1

    随着微服务架构的流行,服务按照不同的维度进行拆分,一次请求往往需要涉及到多个服务.互联网应用构建在不同的软件模块集上,这些软件模块,有可能是由不同的团队开发.可能使用不同的编程语言来实现.有可能布在了 ...

  5. Sc config http start= disabled

    我不小心使用这个命令 Sc config http start= disabled, 现在http服务 无法启动 管理员运行 sc config http start= demand & ne ...

  6. .net core + xunit 集成测试

    xunit地址:https://github.com/xunit/xunit 一.利用请求来测试接口,主要是测试webapi控制器方法 ①添加xunit项目 ,然后引用我们的主项目,nuget: Mi ...

  7. cad.net 读取pc3,pmp 读取pc3打印机文件

    修改pc3文件还没做好..大家先look look怎么读.... 首先弄一个控制台程序, 然后去下载 Ionic.Zip 这个东西...载到控制台...都很简单... 然后就是复制下面代码,看控制台显 ...

  8. this与bind(this) (es5新增)

    this与bind(this) this this指向的是当前函数的作用域(对象实例),有如下的例子 const app = { name: 'xiaoming', log() { console.l ...

  9. xcode添加一个真机设备

    1.首先先安装Xcode并且运行Xcode,点击左角菜单Xcode -> Preferences:点击Accounts+号弹菜单点击Add Apple ID:弹框输入账号密码普通账号行需要发者账 ...

  10. 在RedisTemplate中使用scan代替keys指令

    keys * 这个命令千万别在生产环境乱用.特别是数据庞大的情况下.因为Keys会引发Redis锁,并且增加Redis的CPU占用.很多公司的运维都是禁止了这个命令的 当需要扫描key,匹配出自己需要 ...