Yubico / Yubico.NET.SDK

A YubiKey SDK for .NET developers
Apache License 2.0
96 stars 48 forks source link

[Question]: Establish PIV session with device #81

Open briggst1 opened 2 months ago

briggst1 commented 2 months ago

Is there an existing issue for this?

What is your question?

I am having difficulty with establishing a PIV session with a key that is already plugged in.

Currently, I have code something like the following. Most of the time this works fine. However, sometimes, I get the System.NotSupportedException with the what() indicating that a PIV session could not be created.

Note: I tried using the yubikey listener from the sample code and had similar results: sometimes it would not get PIV session, other times its just fine.

Thoughts? Something I'm doing / not doing?

foreach (var dev in YubiKeyDevice.FindAll())
{
   try {
       System.Threading.Thread.Sleep(125);
       session = new PivSession(dev);
   }
    catch (Yubico.PlatformInterop.SCardException e)
 {
     MessageBox.Show("Error " + e.Message);
     Debug.Write("Error " + e.Message);
     Debug.Write(e.StackTrace);
     return false;
 }
 catch (System.NotSupportedException e)
 {
     MessageBox.Show(e.Message);
     Debug.Write("Error " + e.Message);
     return false;
 }

Context

No response

DennisDyallo commented 1 month ago

Hi @briggst1 and thanks for checking out the SDK.

I'm new to the codebase myself but upon first glance it makes me want to direct you to this SDK article on creating and disposting connections:

https://docs.yubico.com/yesdk/users-manual/sdk-programming-guide/making-a-connection.html#2a-session

Specifically around the opening (and disposing) of a Session. If you follow the steps from this, do you still encounter the same behaviour?

GregDomzalski commented 1 month ago

I suspect this could be a manifestation of #72

DennisDyallo commented 1 month ago

Are you still experiencing problems @briggst1 ?

briggst1 commented 1 month ago

Yes, there are still times where, when the YubiKey is already plugged in, then I start my application, I get the same error. However, instructing the user to remove the key and re-insert, generally works around the problem. With the workaround, it is more of a nuisance / training issue than a blocking issue.

DennisDyallo commented 1 month ago

I see. Well, I'm still getting used to the codebase myself, but I think there's more related work that might address the issue. e.g. #89. Once it's merged you can try the latest develop branch and build it yourself/download and reference directly and see if you still experience the issue.