Search code examples
c#smartcardwinscard

Smart card reader development with .Net technologies


Does anyone know how to monitor the presence of smart card and read the UID value of the card?

Yes I tried lot of examples in web like

A Smart Card Framework for .NET

pcsc-sharp

Monitoring a Smartcard Reader

But No idea how to do it. I can detect the presence of a card and can get the UID separately, but no idea how to combine them in my application:( .

Help me


Solution

  • I figure it out. thought to share with anyone interested.

    I used winscard.dll functions to access card data and communicate with PC.

    Note that I used Mifare 1K cards as NFC tag and reader ACR 122u.

        private string getcardUID()//only for mifare 1k cards
        {
            string cardUID = "";
            byte[] receivedUID = new byte[256];
            Card.SCARD_IO_REQUEST request = new Card.SCARD_IO_REQUEST();
            request.dwProtocol = Card.SCARD_PROTOCOL_T1;
            request.cbPciLength = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Card.SCARD_IO_REQUEST));
            byte[] sendBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 }; //get UID command      for Mifare cards
            int outBytes = receivedUID.Length;
            int status = Card.SCardTransmit(hCard, ref request, ref sendBytes[0], sendBytes.Length, ref request, ref receivedUID[0], ref outBytes);
    
            if (status != Card.SCARD_S_SUCCESS)
            {
                cardUID = "Error";
            }
            else
            {
                cardUID = BitConverter.ToString(receivedUID.Take(4).ToArray()).Replace("-", string.Empty).ToLower();
            }
    
            return cardUID;
        }
    

    For anyone interested I've written step by step guide to achieve this in here.

    Simple NFC reading system for windows

    Enjoy!!!