Beispiel #1
0
        /// <summary>
        /// Will try to connect to the _connectedReader, see if there is a card present, and if so try to read the data.
        /// </summary>
        /// <returns>Either the error message or data from the card.</returns>
        public string TryToReadCard()
        {
            SCardContext context = new SCardContext();

            context.Establish(SCardScope.System);
            SCardReader reader = new SCardReader(context);

            SCardError result = SCardError.InternalError;

            try
            {
                result = reader.Connect(_connectedReader, SCardShareMode.Shared, SCardProtocol.Any);
            }
            catch (Exception)
            {
                context.Dispose();
                reader.Dispose();
                return(SCardHelper.StringifyError(result));
            }

            string message;

            if (result == SCardError.Success)
            {
                string[] readerNames; SCardProtocol protocol; SCardState state; byte[] atr;
                result = reader.Status(out readerNames, out state, out protocol, out atr);

                if (result == SCardError.Success)
                {
                    message = string.Format("Card detected:{0} Protocol: {1}{0} State: {2}{0} ATR: {3}",
                                            Environment.NewLine, protocol, state, BitConverter.ToString(atr ?? new byte[0]));
                }
                else
                {
                    message = string.Format("Unable to read from card.{0}{1}", Environment.NewLine,
                                            SCardHelper.StringifyError(result));
                }
            }
            else
            {
                message = string.Format("No card is detected (or reader reserved by another application){0} {1}",
                                        Environment.NewLine, SCardHelper.StringifyError(result));
            }

            context.Dispose();
            reader.Dispose();

            return(message);
        }
Beispiel #2
0
 protected virtual void Dispose(bool disposing)
 {
     if (Reader != null)
     {
         Reader.Dispose();
     }
 }
Beispiel #3
0
        public void Done()
        {
            try
            {
                if (sam != null)
                {
                    if (sam.IsConnected)
                    {
                        sam.Disconnect(SCardReaderDisposition.Unpower);
                    }

                    sam.Dispose();
                }

                if (reader != null)
                {
                    if (reader.IsConnected)
                    {
                        reader.Disconnect(SCardReaderDisposition.Unpower);
                    }

                    reader.Dispose();
                }
            }
            catch (Exception E)
            {
                // !!!
            }
        }
Beispiel #4
0
        /// <summary>
        /// Tries to connect to the selected reader.
        /// </summary>
        /// <returns>Null if successful. The error message if not.</returns>
        public string StartMonitoringSelectedReader(string readerName)
        {
            if (string.IsNullOrEmpty(readerName))
            {
                return("Reader name is null or empty");
            }
            if (!_readers.Contains(readerName))
            {
                return("The reader does not exist. [Logic Error]");
            }
            _connectedReader = readerName;

            SCardContext context = new SCardContext();

            context.Establish(SCardScope.System);
            SCardReader reader = new SCardReader(context);

            SCardError result = SCardError.InternalError;

            try
            {
                result = reader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any);
            }
            catch (Exception)
            {
                context.Dispose();
                reader.Dispose();
                return(SCardHelper.StringifyError(result));
            }

            _monitor               = new SCardMonitor(new SCardContext(), SCardScope.System, true);
            _monitor.Initialized  += (_cardInitalised);
            _monitor.CardInserted += (_cardInserted);
            _monitor.CardRemoved  += (_cardRemoved);
            _monitor.Start(readerName);

            return(null);
        }
Beispiel #5
0
 public void Dispose()
 {
     reader.Dispose();
     ctx.Dispose();
 }
Beispiel #6
0
        /// <summary>
        /// Will try to connect to _connectedReader and read the card.
        /// </summary>
        /// <returns>Either the data from the card or the error message. Or if 'uidOnly' is true, just the UID prefixed with 'UID^' and ending with '^'</returns>
        public string ReadCard(bool uidOnly = false)
        {
            SCardContext context = new SCardContext();

            context.Establish(SCardScope.System);
            SCardReader reader = new SCardReader(context);

            SCardError result = reader.Connect(_connectedReader, SCardShareMode.Shared, SCardProtocol.Any);

            if (result != SCardError.Success)
            {
                context.Dispose();
                reader.Dispose();
                return(string.Format("No card is detected (or reader reserved by another application){0}{1}",
                                     Environment.NewLine, SCardHelper.StringifyError(result)));
            }

            string[] readerNames; SCardProtocol protocol; SCardState state; byte[] atr;
            result = reader.Status(out readerNames, out state, out protocol, out atr);

            if (result != SCardError.Success)
            {
                context.Dispose();
                reader.Dispose();
                return(string.Format("Unable to read from card.{0}{1}", Environment.NewLine, SCardHelper.StringifyError(result)));
            }

            string message = string.Format("Card detected:{0}Protocol: {1}{0}State: {2}{0}ATR: {3}{0}",
                                           Environment.NewLine, protocol, state, BitConverter.ToString(atr ?? new byte[0]));

            CommandApdu apdu = new CommandApdu(IsoCase.Case2Short, reader.ActiveProtocol)
            {
                CLA         = 0xFF,
                Instruction = InstructionCode.GetData,
                P1          = 0x00,
                P2          = 0x00,
                Le          = 0
            };

            result = reader.BeginTransaction();

            if (result != SCardError.Success)
            {
                context.Dispose();
                reader.Dispose();
                return(string.Format("Cannot start transaction.{0} {1}", Environment.NewLine, SCardHelper.StringifyError(result)));
            }

            SCardPCI recievePci = new SCardPCI();
            IntPtr   sendPci    = SCardPCI.GetPci(reader.ActiveProtocol);

            byte[] recieveBuffer = new byte[256];

            result = reader.Transmit(sendPci, apdu.ToArray(), recievePci, ref recieveBuffer);

            if (result != SCardError.Success)
            {
                context.Dispose();
                reader.Dispose();
                return(string.Format("Cannot transmit data.{0} {1}", Environment.NewLine, SCardHelper.StringifyError(result)));
            }

            var responseApdu = new ResponseApdu(recieveBuffer, IsoCase.Case2Short, reader.ActiveProtocol);

            message += string.Format("SW1: {1}{0}SW2: {2}{0}", Environment.NewLine, responseApdu.SW1, responseApdu.SW2);

            string data = responseApdu.HasData ? BitConverter.ToString(responseApdu.GetData()) : "--";

            if (uidOnly)
            {
                context.Dispose();
                reader.Dispose();
                return(string.Format("UID^{0}^", data));
            }

            message += string.Format("UID: {0}", data);

            reader.EndTransaction(SCardReaderDisposition.Leave);
            reader.Disconnect(SCardReaderDisposition.Reset);

            context.Dispose();
            reader.Dispose();
            return(message);
        }