标题: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. Javascript报错Converting circular structure to JSON

    主要是因为对象的互相引用,怎么样才能造成对象的互相引用呢? var a = {}; var b = {}; a.b = b; b.a = a; 怎么解决,反正我试了很多,最后选择深度clone thi ...

  2. Spring Boot启动命令参数详解及源码分析

    使用过Spring Boot,我们都知道通过java -jar可以快速启动Spring Boot项目.同时,也可以通过在执行jar -jar时传递参数来进行配置.本文带大家系统的了解一下Spring ...

  3. laravel代码规范强制检查

    目录 介绍 代码规范检查与修复 在git commit时自动检查代码规范 后记 介绍 在团队协作开发中,代码规范是必要的.以前的规范都是自己定,然后手动检查,很难做到有效的约束. 现代的PHP,则有得 ...

  4. django orm 外键id返回对应的名称

    原文:https://blog.csdn.net/gghhm/article/details/99652143 当我们在写django的orm模块时,有时候会在用外键关联时,保存的是对应表中的id字段 ...

  5. c# .net 使用Confluent.Kafka针对kafka进行生产和消费

    首先说明一点,像Confluent.Kafka这种开源的组件,三天两头的更新.在搜索引擎搜索到的结果往往用不了,浪费时间.建议以后遇到类似的情况直接看官网给的Demo. 因为搜索引擎搜到的文章,作者基 ...

  6. cad.net 图层隐藏 IsHidden 用法 eDuplicateRecordName 报错

    提要:影响图层显示的主要有:关闭        isOff冻结        IsFrozen 图层隐藏 isHidden视口冻结 FreezeLayersInViewport 今天小博发现了一件事情 ...

  7. JavaScript核心知识点

    一.JavaScript 简介 一.JavaScript语言的介绍:JavaScript是基于对象和原型的一种动态.弱类型的脚本语言 二.JavaScript语言的组成:JavaScript是由核心语 ...

  8. win10 .net framework 3.5 离线安装 不需要外网

    win 10如果安装系统时没有安装.net 3.5 那么在以后安装时就必须联网. win10 .net framework 3.5 离线安装工具: 链接: https://pan.baidu.com/ ...

  9. 039 RabbitMq及数据同步01

    1.RabbitMq (1)问题引出 目前我们已经完成了商品详情和搜索系统的开发.我们思考一下,是否存在问题? 商品的原始数据保存在数据库中,增删改查都在数据库中完成. 搜索服务数据来源是索引库,如果 ...

  10. .net list转树状结构

    主要的方法 /// <summary> /// 转化成树结构 /// </summary> /// <param name="menuList"> ...