示例#1
0
        /// <summary>
        /// Sample code to hande a couple of different cards based on the identification process
        /// </summary>
        /// <returns>None</returns>
        private async Task HandleCard(CardAddedEventArgs args)
        {
            try
            {
                card?.Dispose();
                card = args.SmartCard.CreateMiFareCard();



                var cardIdentification = await card.GetCardInfo();


                DisplayText("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString() + "\r\nCard name: " + cardIdentification.PcscCardName.ToString());

                if (cardIdentification.PcscDeviceClass == MiFare.PcSc.DeviceClass.StorageClass &&
                    (cardIdentification.PcscCardName == CardName.MifareStandard1K || cardIdentification.PcscCardName == CardName.MifareStandard4K))
                {
                    // Handle MIFARE Standard/Classic
                    DisplayText("MIFARE Standard/Classic card detected");


                    var uid = await card.GetUid();

                    DisplayText("UID:  " + BitConverter.ToString(uid));



                    // 16 sectors, print out each one
                    for (var sector = 0; sector < 16; sector++)
                    {
                        try
                        {
                            var data = await card.GetData(sector, 0, 48);

                            string hexString = "";
                            for (int i = 0; i < data.Length; i++)
                            {
                                hexString += data[i].ToString("X2") + " ";
                            }

                            DisplayText(string.Format("Sector '{0}':{1}", sector, hexString));
                        }
                        catch (Exception)
                        {
                            DisplayText("Failed to load sector: " + sector);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                PopupMessage("HandleCard Exception: " + e.Message);
            }
        }
示例#2
0
 private async Task <byte[]> loadBlock(int sector, int block, MiFareCard card)
 {
     try
     {
         if (block < 3)
         {
             return(await card.GetData(sector, block, 16));
         }
         if (block == 3)
         {
             return(await card.GetData(sector, 3, 16));
         }
         if (block == 5)
         {
             return(await card.GetData(sector, 3, 16));
         }
     }
     catch
     {
         return(null);
     }
     return(null);
 }
        public async Task <string> GetData()
        {
            return(await Task.Run(async() =>
            {
                try
                {
                    var s = connection.GetSector(sector);

                    var data = await connection.GetData(sector, 0, s.DataLength);

                    var name = Encoding.UTF8.GetString(data, 0, data.Length);

                    // We're reading the whole sector, remove trailing nulls
                    name = name.Replace("\0", "");
                    return name;
                }
                catch (Exception e)
                {
                    throw new SmartCardException("Could not authenticate to card", e);
                }
            }));
        }