Code Skeleton to Establish Connection Between a .NET Application and the HSM
After all previous configuration steps have been done, it should be now possible to use Pkcs11Interopinterface API to provide the .NET application with an end-to-end connection to Utimaco HSM device. The following shortest code skeleton can be used to establish this bridge:
›_ Console
using Net.Pkcs11Interop. ighLevelAPI;
static void Main(string[] args)
{
// Path to unmanaged library provided by the device vendor
String path = "cs_pkcs11_R2.dll";
// Unmanaged PKCS#11 library is loaded by the constructor of Pkcs11 class.
// Every PKCS#11 library needs to be initialized with C\_Initialize method
// which is also called automatically by the constructor of Pkcs11 class.
Pkcs11 pkcs11 = new Pkcs11(path, false);
// find accessible slot of the token
List<Slot> slots = pkcs11.GetSlotList(true);
Slot slot = slots[0];
Session session = slot.OpenSession(false); String pin = "1234"; session.Login(CKU.CKU_USER, pin); session.Logout(); session.CloseSession();
// Unmanaged PKCS#11 library is unloaded by Dispose() method. // C_Finalize should be the last call made by an application and it
// is also called automatically by Dispose() method.
pkcs11.Dispose();
}