user20202784
user20202784

Reputation:

PCSC Error on access to Smart Card - at first attempt

i have a WPF c# app where the user has to login with his smart Card to athenticate.

In the most cases it is working fine. But i was told that sometimes when placing the smart card on the reader the programm is crashing.

I tryed to reproduce the crashing and error i was telling. I was able to reproduce that error when i place just very short the smart card on the reader. Then i was getting some Unhandled Exceptions, i got some different messages not every time the same.

I don't know if that is really the real error, or if there might be something else.

So here the different errors I'm getting:

enter image description here

enter image description here

enter image description here

Maybe there is also some optimization i can do in the code to avoid that, so here some part of the code:

using Sydesoft.NfcDevice;
namespace BDE
{   
    public partial class MainWindow : lib.Window
    {
     public ACR122U acr122u = new ACR122U();

     public MainWindow()
     {
     InitializeComponent();     
     acr122u.Init(false, 50, 4, 4, 200);  // NTAG213
     acr122u.CardInserted += Acr122u_CardInserted;
     }

     private void Acr122u_CardInserted(PCSC.ICardReader reader)
{
    // Console.WriteLine("NFC Transponder wurde aufgelegt.");
    //Console.WriteLine("Unique ID: " + BitConverter.ToString(acr122u.GetUID(reader)).Replace("-", ""));
    if (objASC == null && objASC2 == null && objCC2 == null && objCCNeo == null && objTF == null && objTF2 == null)
    {
        if (userlogedin == false)
        {
            try
            {
                CardLogin(BitConverter.ToString(acr122u.GetUID(reader)).Replace("-", ""));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        else
        {
            Logout2();
        }
    }
    else
    {
        if (Adminchecked == false)
        {
            if (userlogedin == false)
            {
                try
                {
                    CardLogin(BitConverter.ToString(acr122u.GetUID(reader)).Replace("-", ""));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                Logout2();
            }
        }
        else
        {
            try
            {
                CardLogin(BitConverter.ToString(acr122u.GetUID(reader)).Replace("-", ""));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
private void CardLogin(string name2)
{
    if (objASC == null && objASC2 == null && objCC2 == null && objCCNeo == null && objTF == null && objTF2 == null)
    {                
        SqlConnection con = new SqlConnection(connectionstringCheck);
        con.Open();
        // schauen ob Ausweis eine eRsatzkarte ist, --- überprüfen ob value bei replace = 1 ist, if yes name2 = idSaver
        string queryId = "Select [replace], [idSaver] FROM [KHT-STUECKLISTE].dbo.[Checkliste_Persons] WHERE id = '" + name2 + "'";
        SqlCommand cmdId = new SqlCommand(queryId, con);
        SqlDataReader readerId = cmdId.ExecuteReader();
        if (readerId.Read())
        {
            if (readerId.GetInt32(0) == 1)
            {
                // evtl check if reader = ISDBNULL
                if (!readerId.IsDBNull(1))
                    name2 = readerId.GetString(1);
                else
                { MessageBox.Show("Ausweis kann nicht verwendet werden!"); return; }
            }
        }
        con.Close();
        SqlConnection con1 = new SqlConnection(connectionstringCheck);
        con1.Open();
        name = name2;
        string query = "Select Vorname FROM [KHT-STUECKLISTE].dbo.[Checkliste_Persons] WHERE id = '" + name2 + "'";
        SqlCommand cmd = new SqlCommand(query, con1);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            lib.Application.Current.Dispatcher.Invoke(() => txtName_Check.Text = reader["Vorname"].ToString());
            //lib.Application.Current.Dispatcher.Invoke(() => btnChecklist.Background = new SolidColorBrush(Colors.LightGreen));
            lib.Application.Current.Dispatcher.Invoke(() => btnChecklist.Style = (lib.Style)FindResource("buttonStyleChecklist_Logedin"));

            //activate Timer
            timerLogout = new System.Timers.Timer(300000);
            timerLogout.Elapsed += OnTimedEventCheck;
            timerLogout.AutoReset = false;
            timerLogout.Enabled = true;

            _timeCheck = TimeSpan.FromSeconds(300);

            _timerCheck = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
            {
                //LogoutTime.Text = _timeCheck.ToString("c");
                txttime_Check.Text = _timeCheck.ToString("c");
                if (_timeCheck == TimeSpan.Zero) _timerCheck.Stop();
                _timeCheck = _timeCheck.Add(TimeSpan.FromSeconds(-1));
            }, lib.Application.Current.Dispatcher);
            _timerCheck.Start();
            userlogedin = true;
        }
        else
        {
            lib.MessageBox.Show("ID unbekannt. Bitte melden sie sich bei Ihrem Administrator.");
            //identifikationsnumber.Text = string.Empty;
        }
        con1.Close();
    }
    else
    {
        adminname = name2;
        SqlConnection con = new SqlConnection(connectionstringCheck);
        con.Open();
        string query = "Select Vorname, Supervisor FROM [KHT-STUECKLISTE].dbo.[Checkliste_Persons] WHERE id = '" + name2 + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            if (reader["Supervisor"].ToString() == "1")
            {
                if (objASC != null)
                {
                    // dies passiert wenn Admin button gedrückt ist und der reader den admin NFC tag erkennt
                    lib.Application.Current.Dispatcher.Invoke(() => objASC.btnAdmin.Background = new SolidColorBrush(Colors.Green));
                    lib.Application.Current.Dispatcher.Invoke(() => objASC.Enable());
                    lib.Application.Current.Dispatcher.Invoke(() => objASC.btnAdmin.Content = "Loggout");
                    Adminloggedin = true;
                    objASC.Adminid = name2;
                }
                else if (objASC2 != null)
                {
                    // dies passiert wenn Admin button gedrückt ist und der reader den admin NFC tag erkennt
                    lib.Application.Current.Dispatcher.Invoke(() => objASC2.btnAdmin.Background = new SolidColorBrush(Colors.Green));
                    lib.Application.Current.Dispatcher.Invoke(() => objASC2.Enable());
                    lib.Application.Current.Dispatcher.Invoke(() => objASC2.btnAdmin.Content = "Loggout");
                    Adminloggedin = true;
                    objASC2.Adminid = name2;
                }
                else if (objCC2 != null)
                {
                    // dies passiert wenn Admin button gedrückt ist und der reader den admin NFC tag erkennt
                    lib.Application.Current.Dispatcher.Invoke(() => objCC2.btnAdmin.Background = new SolidColorBrush(Colors.Green));
                    lib.Application.Current.Dispatcher.Invoke(() => objCC2.Enable());
                    lib.Application.Current.Dispatcher.Invoke(() => objCC2.btnAdmin.Content = "Loggout");
                    Adminloggedin = true;
                    objCC2.Adminid = name2;
                }
                else if (objCCNeo != null)
                {
                    // dies passiert wenn Admin button gedrückt ist und der reader den admin NFC tag erkennt
                    lib.Application.Current.Dispatcher.Invoke(() => objCCNeo.btnAdmin.Background = new SolidColorBrush(Colors.Green));
                    lib.Application.Current.Dispatcher.Invoke(() => objCCNeo.Enable());
                    lib.Application.Current.Dispatcher.Invoke(() => objCCNeo.btnAdmin.Content = "Loggout");
                    Adminloggedin = true;
                    objCCNeo.Adminid = name2;
                }
                else if (objTF != null)
                {
                    // dies passiert wenn Admin button gedrückt ist und der reader den admin NFC tag erkennt
                    lib.Application.Current.Dispatcher.Invoke(() => objTF.btnAdmin.Background = new SolidColorBrush(Colors.Green));
                    lib.Application.Current.Dispatcher.Invoke(() => objTF.Enable());
                    lib.Application.Current.Dispatcher.Invoke(() => objTF.btnAdmin.Content = "Loggout");
                    Adminloggedin = true;
                    objTF.Adminid = name2;
                }
                else if (objTF2 != null)
                {
                    // dies passiert wenn Admin button gedrückt ist und der reader den admin NFC tag erkennt
                    lib.Application.Current.Dispatcher.Invoke(() => objTF2.btnAdmin.Background = new SolidColorBrush(Colors.Green));
                    lib.Application.Current.Dispatcher.Invoke(() => objTF2.Enable());
                    lib.Application.Current.Dispatcher.Invoke(() => objTF2.btnAdmin.Content = "Loggout");
                    Adminloggedin = true;
                    objTF2.Adminid = name2;
                }
            }
            else
            {
                MessageBox.Show("Adminloggin war nicht möglich. kein Admin.");
            }
        }
    }

}
    
    }
}

Upvotes: 0

Views: 336

Answers (0)

Related Questions