X500DistinguishedName 类

定义

表示 X509 证书的可分辨名称。Represents the distinguished name of an X509 certificate. 此类不能被继承。This class cannot be inherited.

public ref class X500DistinguishedName sealed : System::Security::Cryptography::AsnEncodedData
public sealed class X500DistinguishedName : System.Security.Cryptography.AsnEncodedData
type X500DistinguishedName = class
    inherit AsnEncodedData
Public NotInheritable Class X500DistinguishedName
Inherits AsnEncodedData
继承
X500DistinguishedName

示例

下面的代码示例演示如何使用 X500DistinguishedName 类。The following code example shows how to use the X500DistinguishedName class.

#using <System.dll>
#using <system.security.dll>

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Security::Permissions;
using namespace System::IO;
using namespace System::Security::Cryptography::X509Certificates;
int main()
{
   try
   {
      X509Store ^ store = gcnew X509Store( "MY",StoreLocation::CurrentUser );
      store->Open( static_cast<OpenFlags>(OpenFlags::ReadOnly | OpenFlags::OpenExistingOnly) );
      X509Certificate2Collection ^ collection = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
      X509Certificate2Collection ^ fcollection = dynamic_cast<X509Certificate2Collection^>(collection->Find( X509FindType::FindByTimeValid, DateTime::Now, false ));
      X509Certificate2Collection ^ scollection = X509Certificate2UI::SelectFromCollection(fcollection, "Test Certificate Select","Select a certificate from the following list to get information on that certificate",X509SelectionFlag::MultiSelection);
      Console::WriteLine( "Number of certificates: {0}{1}", scollection->Count, Environment::NewLine );
      System::Collections::IEnumerator^ myEnum = scollection->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum->Current);
         X500DistinguishedName ^ dname = gcnew X500DistinguishedName( x509->SubjectName );
         Console::WriteLine( "X500DistinguishedName: {0}{1}", dname->Name, Environment::NewLine );
         x509->Reset();
      }
      store->Close();
   }
   catch ( CryptographicException^ ) 
   {
      Console::WriteLine( "Information could not be written out for this certificate." );
   }

}

using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;

class X500Sample
{
    static void Main()
    {
        try
        {
            X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
            X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
            X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
            X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select", "Select a certificate from the following list to get information on that certificate", X509SelectionFlag.MultiSelection);
            Console.WriteLine("Number of certificates: {0}{1}", scollection.Count, Environment.NewLine);
            foreach (X509Certificate2 x509 in scollection)
            {
                X500DistinguishedName dname = new X500DistinguishedName(x509.SubjectName);
                Console.WriteLine("X500DistinguishedName: {0}{1}", dname.Name, Environment.NewLine);
                x509.Reset();
            }
            store.Close();
        }
        catch (CryptographicException)
        {
            Console.WriteLine("Information could not be written out for this certificate.");
        }
    }
}
Imports System.Security.Cryptography
Imports System.Security.Permissions
Imports System.IO
Imports System.Security.Cryptography.X509Certificates



Class X500Sample
   Shared msg As String
   Shared Sub Main()
    
      Try
         Dim store As New X509Store("MY", StoreLocation.CurrentUser)
         store.Open((OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly))
         Dim collection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
         Dim fcollection As X509Certificate2Collection = CType(collection.Find(X509FindType.FindByTimeValid, DateTime.Now, False), X509Certificate2Collection)
         Dim scollection As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select", "Select a certificate from the following list to get information on that certificate", X509SelectionFlag.MultiSelection)
     msg = "Number of certificates: " & scollection.Count & Environment.NewLine
     MsgBox(msg)
         Dim x509 As X509Certificate2
         For Each x509 In  scollection
            Dim dname As New X500DistinguishedName(x509.SubjectName)
        msg = "X500DistinguishedName: " & dname.Name & Environment.NewLine
     MsgBox(msg)
            x509.Reset()
         Next x509
         store.Close()
     Catch e As Exception
            msg = "Error: Information could not be written out for this certificate."
            MsgBox(msg)
      End Try
   End Sub
End Class

注解

此类类似于或属性的扩展 SubjectName IssuerName ,这是要向其颁发证书的个人或实体的名称。This class is like an extension to the SubjectName or IssuerName property, which is the name of the person or entity that the certificate is being issued to. 对于分布式目录服务,X 为国际标准。X.500 is an international standard for distributed directory services. 可分辨名称使用以下格式:The distinguished name uses the following format:

[X500:/C = CountryCode/O = 组织/OU = OrganizationUnit/CN = CommonName][X500:/C=CountryCode/O=Organization/OU=OrganizationUnit/CN=CommonName]

以下字段长度限制适用:The following field length limits apply:

字段Field 长度限制Length Limit
CountryCodeCountryCode 2个字符2 characters
组织Organization 最多64个字符Up to 64 characters
OrganizationUnitOrganizationUnit 最多32个字符Up to 32 characters
CommonNameCommonName 最多64个字符Up to 64 characters

以下准则将适用:The following guidelines apply:

  • 字段值可以是大写或小写。Field values can be uppercase or lowercase.

  • 分隔符是 (/) 的斜杠标记,并且必须出现在第一个值之前和后面的值之间。Delimiters are slash marks (/) and must appear before the first value and between subsequent values.

  • 分隔符后面可以跟一个空格。Delimiters can be followed by a space.

下面是格式正确的名称的示例: [X500:/C = 美制/O = Microsoft/OU = WGA/CN = TedSt]The following is an example of a correctly formatted name: [X500:/ C=US/ O=Microsoft/ OU=WGA/ CN=TedSt]

构造函数

X500DistinguishedName(AsnEncodedData)

使用指定的 AsnEncodedData 对象初始化 X500DistinguishedName 类的新实例。Initializes a new instance of the X500DistinguishedName class using the specified AsnEncodedData object.

X500DistinguishedName(Byte[])

使用指定字节数组中的信息初始化 X500DistinguishedName 类的新实例。Initializes a new instance of the X500DistinguishedName class using information from the specified byte array.

X500DistinguishedName(ReadOnlySpan<Byte>)

使用所提供数据中的信息初始化 X500DistinguishedName 类的新实例。Initializes a new instance of the X500DistinguishedName class using information from the provided data.

X500DistinguishedName(String)

使用指定字符串中的信息初始化 X500DistinguishedName 类的新实例。Initializes a new instance of the X500DistinguishedName class using information from the specified string.

X500DistinguishedName(String, X500DistinguishedNameFlags)

使用指定的字符串和 X500DistinguishedName 标志初始化 X500DistinguishedNameFlags 类的新实例。Initializes a new instance of the X500DistinguishedName class using the specified string and X500DistinguishedNameFlags flag.

X500DistinguishedName(X500DistinguishedName)

使用指定的 X500DistinguishedName 对象初始化 X500DistinguishedName 类的新实例。Initializes a new instance of the X500DistinguishedName class using the specified X500DistinguishedName object.

属性

Name

从 X500 证书获取逗号分隔的识别名。Gets the comma-delimited distinguished name from an X500 certificate.

Oid

获取或设置 Oid 对象的 AsnEncodedData 值。Gets or sets the Oid value for an AsnEncodedData object.

(继承自 AsnEncodedData)
RawData

获取或设置以字节数组表示的 Abstract Syntax Notation One (ASN.1) 编码数据。Gets or sets the Abstract Syntax Notation One (ASN.1)-encoded data represented in a byte array.

(继承自 AsnEncodedData)

方法

CopyFrom(AsnEncodedData)

AsnEncodedData 对象复制信息。Copies information from an AsnEncodedData object.

(继承自 AsnEncodedData)
Decode(X500DistinguishedNameFlags)

使用由 flag 参数指定的特征对识别名进行解码。Decodes a distinguished name using the characteristics specified by the flag parameter.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Format(Boolean)

返回 X500 识别名的格式化版本,以便打印或输出到文本窗口或控制台。Returns a formatted version of an X500 distinguished name for printing or for output to a text window or to a console.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于