第一次尝试无法访问智能卡,任何其他成功

时间:2016-02-02 11:55:16

标签: c# smartcard pcsc gemalto

我在使用PCSC阅读器和智能卡时遇到了问题。我无法使用GUI应用程序访问该卡。它就像控制台示例应用程序中的魅力一样 我收到例外:

while

但是当我取出卡并重新插入时,它可以正常工作 我认为在插入时我的Windows机器中的其他进程访问了该卡,所以我创建了一个ret,当-2146435061值等于{{ 1}}在WinSCardExceptioncontinue循环中,或break如果连接正常。
我要做的步骤是连接到卡片:

PCSCReader reader = new PCSCReader();
string[] readers = reader.SCard.ListReaders(); 
// Returns 3 readers (even though I have 2 connected, but when I once connected the third one it now appears always) - why?
// Here with GUI I choose interested reader (which is really connected)
reader.SCard.ReleaseContext();
reader.Disconnect(); // In case there is any reader connected
// Here I stop my worker so that It will not try to access reader when it is not connected
reader.Connect(readers[1]); // For example let's connect to reader 1
// Now the worker starts working

//...DoWork method of worker:
while(true)
{
  try {reader.ActivateCard(); break;} // break if successfully connected
  // If the ex status is positive then there is some other issue which is handled by bigger try-catch, but for case ret is -2146435061 i want to continue the loop
  catch (WinSCardException ex) {if (ex.Status > -100) throw (ex); }
  // But this throw Exception over and over again

请帮助。
我使用这个包装器:http://www.smartcard-magic.net/en/pc-sc-reader/csharppcsc-wrapper/
示例程序看起来几乎相同,但不会抛出任何错误。

using System;
using System.Diagnostics;
using GS.Apdu;
using GS.PCSC;
using GS.SCard;
using GS.SCard.Const;
using GS.Util.Hex;

namespace ExamplePCSCReader
{
    class Program
    {
        static void Main( string[] args )
        {
            ConsoleTraceListener consoleTraceListener = new ConsoleTraceListener();
            Trace.Listeners.Add(consoleTraceListener);

            PCSCReader reader = new PCSCReader();

            try
            {
                reader.Connect();
                reader.ActivateCard();

                RespApdu respApdu = reader.Exchange("00 B0 00 00 0A"); // Get Card UID ...
                if (respApdu.SW1SW2 == 0x9000)
                {
                    Console.WriteLine("ICCID  = 0x" + HexFormatting.ToHexString(respApdu.Data, true));
                }
            }
            catch (WinSCardException ex)
            {
                Console.WriteLine( ex.WinSCardFunctionName + " Error 0x" + 
                                   ex.Status.ToString( "X08" ) + ": " + ex.Message );
            }
            catch (Exception ex)
            {
                Console.WriteLine( ex.Message );
            }
            finally
            {
                reader.Disconnect();
                Console.WriteLine( "Please press any key..." );
                Console.ReadLine();
            }
        }
    }
}
}

3 个答案:

答案 0 :(得分:2)

关于访问阅读器的问题 - 解决方案是与命令共享阅读器:

reader.ActivateCard(GS.SCard.Const.SCARD_SHARE_MODE.Shared, GS.SCard.Const.SCARD_PROTOCOL.Tx);

列出而非列出读者的问题是因为如果没有建立上下文,则包装器会从Windows注册表中获取读者列表。当我在列出读者之前建立上下文时 - 只显示连接的读者。

reader.SCard.EstablishContext();
readers = reader.SCard.ListReaders();

答案 1 :(得分:0)

我和你有同样的问题...但我找到了解决方案。

从SCardShareMode.Exclusive更改为> SCardShareMode.Shared

            _hContext = new SCardContext();
            _hContext.Establish(SCardScope.System);

            // Create a _reader object using the existing context
            _reader = new SCardReader(_hContext);

            // Connect to the card
            if (readerName == null || readerName == String.Empty)
            {
                // Retrieve the list of Smartcard _readers
                string[] szReaders = _hContext.GetReaders();
                if (szReaders.Length <= 0)
                    throw new PCSCException(SCardError.NoReadersAvailable,
                        "Could not find any Smartcard _reader.");

                _err = _reader.Connect(szReaders[0],
                            SCardShareMode.Shared,
                            SCardProtocol.T0 | SCardProtocol.T1);
                CheckErr(_err);
             }

答案 2 :(得分:0)

我知道这个主题很旧,但它可能会帮助将来会遇到此问题的其他人。共享阅读器是一种解决方法,不是解决方案。实际上,由于其他出色的连接而无法以独占模式进行连接这一事实意味着Windows进程正在应用程序外部的智能卡上执行操作。 这可能会导致您的应用程序访问智能卡时出现问题,特别是如果同时使用多个读卡器/卡,则尤其如此。

一个真正的解决方法是在Windows计算机上工作时在组策略编辑器中更改某些配置。它看起来应该像这样: enter image description here

然后,读取器应该能够以独占模式或直接模式连接到卡

欢呼

相关问题