CertificateHolderCreate Method (Byte, SecureString)

Creates CertificateHolder object using byte array of PKCS12 store and its password.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public static CertificateHolder Create(
	byte[] certBytes,
	SecureString password
)

Parameters

certBytes
Type: SystemByte
A byte array that contains data from an X.509 certificate.
password
Type: System.SecuritySecureString
The password required to access the X.509 certificate data.

Return Value

Type: CertificateHolder
An instance of CertificateHolder
Exceptions
ExceptionCondition
InvalidParameterExceptionThrown if certBytes is null
InvalidParameterExceptionThrown if password is null
SecurityExceptionThrown if PKCS12 store contains no aliases
IOExceptionThrown if there is wrong password or corrupted file.
Examples
Shows how to create CertificateHolder objects.
// 1: Load a PKCS #12 file into a byte array and apply its password to create the CertificateHolder
byte[] certBytes = File.ReadAllBytes(MyDir + "morzal.pfx");
CertificateHolder.Create(certBytes, "aw");

// 2: Pass a SecureString which contains the password instead of a normal string
SecureString password = new NetworkCredential("", "aw").SecurePassword;
CertificateHolder.Create(certBytes, password);

// 3: If the certificate has private keys corresponding to aliases, we can use the aliases to fetch their respective keys
// First, we'll check for valid aliases like this
using (FileStream certStream = new FileStream(MyDir + "morzal.pfx", FileMode.Open))
{
    Pkcs12Store pkcs12Store = new Pkcs12Store(certStream, "aw".ToCharArray());
    IEnumerator enumerator = pkcs12Store.Aliases.GetEnumerator();

    while (enumerator.MoveNext())
    {
        if (enumerator.Current != null)
        {
            string currentAlias = enumerator.Current.ToString();
            if (pkcs12Store.IsKeyEntry(currentAlias) && pkcs12Store.GetKey(currentAlias).Key.IsPrivate)
            {
                Console.WriteLine($"Valid alias found: {enumerator.Current}");
            }
        }
    }
}

// For this file, we'll use an alias found above
CertificateHolder.Create(MyDir + "morzal.pfx", "aw", "c20be521-11ea-4976-81ed-865fbbfc9f24");

// If we leave the alias null, then the first possible alias that retrieves a private key will be used
CertificateHolder.Create(MyDir + "morzal.pfx", "aw", null);
See Also