Net.Pkcs11Interop.LowLevelAPI40.Pkcs11.C_OpenSession C# (CSharp) Method

C_OpenSession() public method

Opens a session between an application and a token in a particular slot
public C_OpenSession ( uint slotId, uint flags, IntPtr application, IntPtr notify, uint &session ) : CKR
slotId uint The ID of the token's slot
flags uint Flags indicating the type of session
application System.IntPtr An application defined pointer to be passed to the notification callback
notify System.IntPtr The address of the notification callback function
session uint Location that receives the handle for the new session
return CKR
        public CKR C_OpenSession(uint slotId, uint flags, IntPtr application, IntPtr notify, ref uint session)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            uint rv = _delegates.C_OpenSession(slotId, flags, application, notify, ref session);
            return (CKR)rv;
        }

Usage Example

        public void _01_ReadOnlySessionTest()
        {
            if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 0)
                Assert.Inconclusive("Test cannot be executed on this platform");

            CKR rv = CKR.CKR_OK;
            
            using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath))
            {
                rv = pkcs11.C_Initialize(Settings.InitArgs40);
                if ((rv != CKR.CKR_OK) && (rv != CKR.CKR_CRYPTOKI_ALREADY_INITIALIZED))
                    Assert.Fail(rv.ToString());
                
                // Find first slot with token present
                uint slotId = Helpers.GetUsableSlot(pkcs11);

                // Open RO (read-only) session
                uint session = CK.CK_INVALID_HANDLE;
                rv = pkcs11.C_OpenSession(slotId, CKF.CKF_SERIAL_SESSION, IntPtr.Zero, IntPtr.Zero, ref session);
                if (rv != CKR.CKR_OK)
                    Assert.Fail(rv.ToString());
                
                // Do something interesting in RO session
                
                rv = pkcs11.C_CloseSession(session);
                if (rv != CKR.CKR_OK)
                    Assert.Fail(rv.ToString());

                rv = pkcs11.C_Finalize(IntPtr.Zero);
                if (rv != CKR.CKR_OK)
                    Assert.Fail(rv.ToString());
            }
        }
All Usage Examples Of Net.Pkcs11Interop.LowLevelAPI40.Pkcs11::C_OpenSession