1

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.

jariq
  • 11,681
  • 3
  • 33
  • 52
Kumar
  • 63
  • 1
  • 10

1 Answers1

1

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.

jariq
  • 11,681
  • 3
  • 33
  • 52
  • Thanks @jariq for you quick resolution you are always a saviour. – Kumar Jun 30 '17 at 09:05
  • I have one more issue if you can help. In my class I always log in into HSM in constructor and log out in dispose method. However, if some exception comes in between it always starts throwing error that user is already logged in into HSM. My application is an multi threaded application. Please help what should I do. – Kumar Jun 30 '17 at 09:12
  • In the catch block I have written session.Dispose() should I add session.Logout(). Or is there any method using which can tell me whether the user is already logged in. Then I can check and if he is not logged in then only I will call the Login method. Thanks in Advance – Kumar Jun 30 '17 at 09:13
  • 1
    @Kumar you can use `session.GetSessionInfo().State` to check whether it is a public or authenticated session. – jariq Jun 30 '17 at 10:20
  • Thanks a lot jariq – Kumar May 17 '18 at 08:49