Bonjour,

Je souhaite utiliser ASP NET pour me connecter à un lecteur smartcard
connecté à un PC.

Principe : Le serveur web founit un asp qui est utilisé en local pour se connecter à un lecteur smartcard.

J'ai écris le code suivant qui permet de lister les lecteurs pcsc connectés au PC
via le navigateur lorsqu'on click sur Button1_Click. Cela fonctionne.

Ma question : les lecteurs smartard listés sont t'ils ceux connectés sur le serveur web ou ceux de mon PC en local ?

default.aspx :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>test</title>
</head>
<body>Please enter you login and password below, you will be authenticate        <form id="form1" runat="server">
    <div>
 
    </div>
    <asp:Login ID="Login1" runat="server" onauthenticate="Login1_Authenticate">
    </asp:Login>
    <asp:ListBox ID="ListBox1" runat="server" 
        onselectedindexchanged="ListBox1_SelectedIndexChanged" Width="136px">
    </asp:ListBox>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="List readers" 
        onclick="Button1_Click" />
    </form>
</body>
</html>

default.aspx.cs

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Data;
// Neded for [DllImport] 
using System.Runtime.InteropServices;
 
using System.Windows.Forms;
using System.Text;
 
public partial class _Default : System.Web.UI.Page 
{
    private int nContext;				//Card reader context handle - DWORD
    // private int nCard;
    // private int nActiveProtocol;        //T0/T1 
 
 
    [StructLayout(LayoutKind.Sequential)] 
    internal class SCARD_IO_REQUEST
    {
    internal uint dwProtocol;
    internal int cbPciLength;
    public SCARD_IO_REQUEST()
      {
        dwProtocol = 0;
      }
    }
 
 
    // WinSCard API's to be imported
    [DllImport("WinScard.dll")]
    public static extern int SCardEstablishContext(uint dwScope, int nNotUsed1,
        int nNotUsed2, ref int phContext);
    [DllImport("WinScard.dll")]
    public static extern int SCardReleaseContext(int phContext);
    [DllImport("WinScard.dll")]
    public static extern int SCardConnect(int hContext, string cReaderName,
        uint dwShareMode, uint dwPrefProtocol, ref int phCard, ref int ActiveProtocol);
    [DllImport("WinScard.dll")]
    public static extern int SCardDisconnect(int hCard, int Disposition);
    [DllImport("WinScard.dll")]
    public static extern int SCardListReaderGroups(int hContext, ref string cGroups, ref int nStringSize);
 
    [DllImport("winscard.dll")]
    public static extern int SCardListReaders(int hContext, string cGroups, byte[] cReaderLists, ref int nReaderCount); 
 
    [DllImport("WinScard.dll")]
    public static extern int SCardFreeMemory(int hContext, string cResourceToFree);
    [DllImport("WinScard.dll")]
    public static extern int SCardGetAttrib(int hContext, uint dwAttrId,
        ref byte[] bytRecvAttr, ref int nRecLen);
    [DllImport("winscard.dll")]
    static extern int SCardConnect(int hContext,[MarshalAs(UnmanagedType.LPTStr)] string szReader,
           UInt32 dwShareMode, 
            UInt32 dwPreferredProtocols, 
            out int phCard, 
            out UInt32 pdwActiveProtocol);
 
 
    [DllImport("WinScard.dll")]
    static extern int SCardTransmit(int hCard, IntPtr pioSendPci, byte[] pbSendBuffer, int cbSendLength, SCARD_IO_REQUEST pioRecvPci, 
        byte[] pbRecvBuffer, ref int pcbRecvLength);
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
        MessageBox.Show("start");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // MessageBox.Show("Button click");
        //First step in using smart cards is CSardEstablishContext()
        uint nContext = 2;		//system
        int nNotUsed1 = 0;
        int nNotUsed2 = 0;
        this.nContext = 0;		//handle to context - SCARDCONTEXT
 
        int nRetVal1 = SCardEstablishContext(nContext, nNotUsed1, nNotUsed2, ref this.nContext);
        if (nRetVal1 != 0)
        {
            MessageBox.Show("Error returned by SCardEstablishContext()");
            return;
        }
 
        //next we split up the null delimited strings into a string array
        char[] delimiter = new char[1];
        delimiter[0] = Convert.ToChar(0);
 
        //Next we need to call the  SCardListReaderGroups() to find reader groups to use
        string cGroupList = "" + Convert.ToChar(0);
        int nStringSize = -1;	//SCARD_AUTOALLOCATE
        int nRetVal2 = SCardListReaderGroups(this.nContext, ref cGroupList, ref nStringSize);
        if (nRetVal2 != 0)
        {
            MessageBox.Show("Error returned by SCardListReaderGroups()");
            return;
        }
 
        string[] cGroups = cGroupList.Split(delimiter);
 
        int nReaderCount = -1;
 
        // Get the reader list
		int nRetVal4 = SCardListReaders(this.nContext, cGroups[0], null, ref nReaderCount);
        if (nRetVal4 != 0)
        {
            MessageBox.Show(" Error returned by SCardListReaders()");
            return;
        }
 
        byte[] cReaderList = new byte[nReaderCount];
 
        nRetVal4 = SCardListReaders(this.nContext, cGroups[0], cReaderList, ref nReaderCount);
        if (nRetVal4 != 0)
        {
            MessageBox.Show(" Error returned by SCardListReaders()");
            return;
        }
 
 
        ASCIIEncoding asc = new ASCIIEncoding();
        String[] cReaders = asc.GetString(cReaderList).Split('\0');
 
        foreach (string cName in cReaders)
        {
            MessageBox.Show("Found Reader Name: " + cName, "Found Reader");
            // listBox1.Items.Add(cName);
        }
		// listBox1.Items.Clear();
 
 
	}
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
 
    }
 
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
    }
}
Merci.