Search code examples
c#pkcs#11hsmpkcs11interop

Which one should I use: session vs _session in Pkcs11interop


I am facing the same issue as the owner of this thread: Threadsafe usage of PKCS11Interop library in C#

Here you have provided a solution and given a link of https://github.com/jariq/Pkcs11Interop.PDF/blob/1.3.0/src/Pkcs11Interop.PDF/Pkcs11RsaSignature.cs

I have a small confusion, in this class I have seen many occurrences where you are using

using (Session session = _slot.OpenSession(true))

statement. However inside it, you are using the predefined variable _session, you are not using this session. But in the Sign method you are using this session instead of _session. Please help, in which cases I should use session and _session.


Solution

  • Thanks for spotting this. Local variable session should always be preferred. Field _session should be used only to keep the login state. I've fixed the code in commit 179003b and fixed class is available in master branch.