語言

AsnEncodedData 類別

定義

代表抽象語法符號 ASN.1 編碼資料。

public ref class AsnEncodedData
public class AsnEncodedData
type AsnEncodedData = class
Public Class AsnEncodedData
繼承
AsnEncodedData
衍生

範例

以下程式碼範例說明如何使用這個 AsnEncodedData 類別。

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

class AsnEncodedDataSample
{
    static void Main()
    {		
        //The following example demonstrates the usage the AsnEncodedData classes.
        // Asn encoded data is read from the extensions of an X509 certificate.
        try
        {
            // Open the certificate store.
            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);
            // Select one or more certificates to display extensions information.
            X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Certificate Select", "Select certificates from the following list to get extension information on that certificate", X509SelectionFlag.MultiSelection);

            // Create a new AsnEncodedDataCollection object.
            AsnEncodedDataCollection asncoll = new AsnEncodedDataCollection();
            for (int i = 0; i < scollection.Count; i++)
            {
                // Display certificate information.
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Certificate name: {0}", scollection[i].GetName());
                Console.ResetColor();
                // Display extensions information.
                foreach (X509Extension extension in scollection[i].Extensions)
                {
                    // Create an AsnEncodedData object using the extensions information.
                    AsnEncodedData asndata = new AsnEncodedData(extension.Oid, extension.RawData);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Extension type: {0}", extension.Oid.FriendlyName);
                    Console.WriteLine("Oid value: {0}",asndata.Oid.Value);
                    Console.WriteLine("Raw data length: {0} {1}", asndata.RawData.Length, Environment.NewLine);
                    Console.ResetColor();
                    Console.WriteLine(asndata.Format(true));
                    Console.WriteLine(Environment.NewLine);
                    // Add the AsnEncodedData object to the AsnEncodedDataCollection object.
                    asncoll.Add(asndata);
                }
                Console.WriteLine(Environment.NewLine);
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Number of AsnEncodedData items in the collection: {0} {1}", asncoll.Count, Environment.NewLine);
            Console.ResetColor();

            store.Close();
            //Create an enumerator for moving through the collection.
            AsnEncodedDataEnumerator asne = asncoll.GetEnumerator();
            //You must execute a MoveNext() to get to the first item in the collection.
            asne.MoveNext();
            // Write out AsnEncodedData in the collection.
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("First AsnEncodedData in the collection: {0}", asne.Current.Format(true));
            Console.ResetColor();

            asne.MoveNext();
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("Second AsnEncodedData in the collection: {0}", asne.Current.Format(true));
            Console.ResetColor();
            //Return index in the collection to the beginning.
            asne.Reset();
        }
        catch (CryptographicException)
        {
            Console.WriteLine("Information could not be written out for this certificate.");
        }
    }
}
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates



Class AsnEncodedDataSample
   Shared msg As String
   Shared Sub Main()
      'The following example demonstrates the usage the AsnEncodedData classes.
      ' Asn encoded data is read from the extensions of an X509 certificate.
      Try
         ' Open the certificate store.
         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)
         ' Select one or more certificates to display extensions information.
         Dim scollection As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(fcollection, "Certificate Select", "Select certificates from the following list to get extension information on that certificate", X509SelectionFlag.MultiSelection)
         
         ' Create a new AsnEncodedDataCollection object.
         Dim asncoll As New AsnEncodedDataCollection()
         Dim i As Integer
         For i = 0 To scollection.Count - 1
            ' Display certificate information.
        msg = "Certificate name: "& scollection(i).GetName()
            MsgBox(msg)

            ' Display extensions information.
            Dim extension As X509Extension
            For Each extension In  scollection(i).Extensions
               ' Create an AsnEncodedData object using the extensions information.
               Dim asndata As New AsnEncodedData(extension.Oid, extension.RawData)
           msg = "Extension type: " & extension.Oid.FriendlyName & Environment.NewLine & "Oid value: " & asndata.Oid.Value _
        & Environment.NewLine & "Raw data length: " & asndata.RawData.Length & Environment.NewLine _
        & asndata.Format(True) & Environment.NewLine
               MsgBox(msg)
        
               ' Add the AsnEncodedData object to the AsnEncodedDataCollection object.
               asncoll.Add(asndata)
            Next extension
         Next i
     msg = "Number of AsnEncodedData items in the collection: " & asncoll.Count
         MsgBox(msg)         
         store.Close()

         'Create an enumerator for moving through the collection.
         Dim asne As AsnEncodedDataEnumerator = asncoll.GetEnumerator()
         'You must execute a MoveNext() to get to the first item in the collection.
         asne.MoveNext()
         ' Write out AsnEncodedData in the collection.
     msg = "First AsnEncodedData in the collection: " & asne.Current.Format(True)
     MsgBox(msg)
    
         
         asne.MoveNext()
     msg = "Second AsnEncodedData in the collection: " & asne.Current.Format(True)
     MsgBox(msg)
        
         'Return index in the collection to the beginning.
         asne.Reset()
      Catch 
         MsgBox("Information could not be written out for this certificate.")
      End Try
   End Sub
End Class

備註

抽象語法符號一(ASN.1)定義於 CCITT 建議書 X.208,是一種用來指定將以序列方式傳送的抽象物件的方法。 ASN.1 中用來表示此類物件為 1 與 0 字串的規則集合稱為區別編碼規則(Distinguished Encoding Rules,DER),定義於 CCITT 建議書 X.509,第 8.7 節。 這些編碼方法目前被 .NET 框架中的密碼命名空間所使用。

請注意,若在存取此類實例時遇到未知資料型態,資料會以十六進位字串形式回傳。

建構函式

名稱 Description
AsnEncodedData()

初始化 AsnEncodedData 類別的新執行個體。

AsnEncodedData(AsnEncodedData)

使用該類別的實例AsnEncodedData初始化該類別的新實例AsnEncodedData。

AsnEncodedData(Byte[])

使用位元組陣列初始化該類別的新實例 AsnEncodedData 。

AsnEncodedData(Oid, Byte[])

使用AsnEncodedData物件與位元組陣列初始化該類別的新實例Oid。

AsnEncodedData(Oid, ReadOnlySpan<Byte>)

從物件識別碼(OID)及現有編碼資料初始化該類別的新實例 AsnEncodedData 。

AsnEncodedData(ReadOnlySpan<Byte>)

從現有編碼資料初始化該類別的新實例 AsnEncodedData 。

AsnEncodedData(String, Byte[])

使用位元組陣列初始化該類別的新實例 AsnEncodedData 。

AsnEncodedData(String, ReadOnlySpan<Byte>)

從物件識別碼(OID)及現有編碼資料初始化該類別的新實例 AsnEncodedData 。

屬性

名稱 Description
Oid

取得或設定 Oid 物件 AsnEncodedData 的值。

RawData
已淘汰.

取得或設定以位元組陣列表示的抽象語法符號 1(ASN.1)編碼資料。

方法

名稱 Description
CopyFrom(AsnEncodedData)

從物件複製資訊 AsnEncodedData 。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Format(Boolean)

以字串形式回傳 Abstract Syntax Notation One (ASN.1) 編碼資料的格式化版本。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於