X509Store 类
标题: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、 Location和Certificates属性显示存储区中的证书数量。
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 类的更多相关文章
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- 转: 在.NET中操作数字证书
作者:玄魂出处:博客2010-06-23 12:05 http://winsystem.ctocio.com.cn/19/9492019.shtml .NET为我们提供了操作数字证书的两个主要的类,分 ...
- 用微软makecert.exe生成一个自签名的证书
RT makecert.exe不用去找,安装VS2008后,在开始菜单相应的路径找到该命令提示符:Microsoft Visual Studio 2008/Visual Studio Tools/Vi ...
- 用makecert.exe生成一个自签名的证书
makecert.exe不用去找,安装VS2008后,在开始菜单相应的路径找到该命令提示符:Microsoft Visual Studio 2008/Visual Studio Tools/Visua ...
- C#获取数字证书
string Thumbprint = "C2489D912F247C187AA14B1291A6fB612281225D"; X509Store store = new X509 ...
- 使用makecert.exe创建数字证书
RT makecert.exe不用去找,安装VS2008后,在开始菜单相应的路径找到该命令提示符:Microsoft Visual Studio 2008/Visual Studio Tools/Vi ...
- 【进阶修炼】——改善C#程序质量(7)
113,声明变量时考虑最大值. Ushort的最大值是65535,用于不同的用途这个变量可能发生溢出,所以设计时应充分了解每个变量的最大值. 114,MD5不再安全. MD5多用于信息完整性的校验.R ...
- 在.NET中操作数字证书(新手教程)
.NET为我们提供了操作数字证书的两个主要的类,分为为: System.Security.Cryptography.X509Certificates.X509Certificate2类, 每个这个类的 ...
- windows下如何制作和应用数字签名证书 全流程
目前我们在发布应用程序时,有时用户下载后会被360杀毒当做木马直接隔离.为应用程序可执行文件打上数字签名可以让360杀毒放宽检测规则.下文是讲述如何制作数字签名证书的过程. 需要准备的工具:makec ...
随机推荐
- java 调用腾讯身份OCR接口文档实例(绝对可用)
1.情景展示 通过读取身份证照片上的信息,实现自动填充功能. 2.原因分析 想要解析照片上所携带的相关信息,就需要识别照片的功能,腾讯提供了免费的身份证OCR接口,可供大家使用. 没有耐心的可以直 ...
- 串口调试工具与com口编程
当我们要与称打印机进行交互的时候这就需要 驱动 只有得到对应产品的驱动我们才有机会对其进行 com 口编程 首先我们就需要一个小工具来进行测试 获取对应的com 口,观察数据传输方式 链接地址: 链 ...
- 045 用户登录功能01----JWT和后台代码
(1)有状态登录概述 有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session. 例如登录:用户登录后,我们把登录者 ...
- Java自学-类和对象 类属性
Java的类属性和对象属性 当一个属性被static修饰的时候,就叫做类属性,又叫做静态属性 当一个属性被声明成类属性,那么所有的对象,都共享一个值 与对象属性对比: 不同对象的 对象属性 的值都可能 ...
- Scala 函数式编程
将函数赋值给变量 // Scala中的函数是一等公民,可以独立定义,独立存在,而且可以直接将函数作为值赋值给变量 // Scala的语法规定,将函数赋值给变量时,必须在函数后面加上空格和下划线 def ...
- 【题解】Luogu P4091 [HEOI2016/TJOI2016]求和
原题传送门 \[\begin{aligned} a n s &=\sum_{i=0}^{n} \sum_{j=0}^{i}\left\{\begin{array}{c}{i} \\ {j}\e ...
- Java的常用API
Object类 1.toString方法在我们直接使用输出语句输出对象的时候,其实通过该对象调用了其toString()方法. 2.equals方法方法摘要:类默认继承了Object类,所以可以使用O ...
- C#项目 App.config 配置文件不同使用环境配置
问题 部署项目时,常常需要根据不同的环境使用不同的配置文件.例如,在部署网站时可能希望禁用调试选项,并更改连接字符串以使其指向不同的数据库.在创建 Web 项目时,Visual Studio 自动生成 ...
- java.net.URLEncoder对中文的编码和解码
// java.net.URLEncoder对中文的编码和解码String str = URLEncoder.encode("测试字符串", "utf-8"); ...
- Linux命令:scp
1.find命令: scp [参数] [原路径] [目标路径] 2.用法: scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令.linux的sc ...