PCSC 6.1.3
dotnet add package PCSC --version 6.1.3
NuGet\Install-Package PCSC -Version 6.1.3
<PackageReference Include="PCSC" Version="6.1.3" />
paket add PCSC --version 6.1.3
#r "nuget: PCSC, 6.1.3"
// Install PCSC as a Cake Addin
#addin nuget:?package=PCSC&version=6.1.3
// Install PCSC as a Cake Tool
#tool nuget:?package=PCSC&version=6.1.3
PC/SC wrapper classes for .NET
Introduction
The pcsc-sharp library is wrapper that provides access to the Personal Computer/Smart Card Resource Manager using the system's native PC/SC API. It implements partial ISO7816 support and is written to run on both Windows and Unix (.Net Core or Mono using PCSC Lite).
pcsc-sharp is not a fully featured library for accessing vendor specific protocols. You must implement those protocols / applications yourself. For example: You can use pcsc-sharp to access NXP's Mirfare RFID chips, but pcsc-sharp does not provide any APDUs to request KEYs, authorize, etc.
pcsc-sharp does not contain any device drivers. A PC/SC compliant reader + driver is mandatory.
The GitHub issue tracker is not a support forum. Please contact your vendor for reader or card specific questions.
You can find PC/SC specific documentation here:
- Windows: Smart Card Resource Manager API
- Linux: PCSC lite project
Supported Operating systems
Windows (winscard.dll)
- Windows 10 64-bit Professional
- Windows 10 32-bit Professional
- Windows 7 64-bit
- Windows 7 32-bit
Linux (PC/SC lite)
- Ubuntu Linux 64-bit
- Ubuntu Linux 32-bit
MacOS X
Raspberry Pi / Linux ARM
- linux-arm
Quick start
Establish the resource manager context
Each operation requires a valid context. See SCardEstablishContext for more information.
var contextFactory = ContextFactory.Instance;
using (var context = contextFactory.Establish(SCardScope.System)) {
// your code
}
Basic rules / best practices:
- One context per smartcard / reader.
- One context per
SCardMonitor
. - The context must be disposed of after usage to free system resources.
- The context must not be disposed of if your application still accesses the smartcard / reader.
List all connected smartcard readers
See SCardListReaders.
var contextFactory = ContextFactory.Instance;
using (var context = contextFactory.Establish(SCardScope.System)) {
Console.WriteLine("Currently connected readers: ");
var readerNames = context.GetReaders();
foreach (var readerName in readerNames) {
Console.WriteLine("\t" + readerName);
}
}
Send ISO7816 APDUs
var contextFactory = ContextFactory.Instance;
using (var ctx = contextFactory.Establish(SCardScope.System)) {
using (var isoReader = new IsoReader(ctx, "ACME Smartcard reader", SCardShareMode.Shared, SCardProtocol.Any, false)) {
var apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol) {
CLA = 0x00, // Class
Instruction = InstructionCode.GetChallenge,
P1 = 0x00, // Parameter 1
P2 = 0x00, // Parameter 2
Le = 0x08 // Expected length of the returned data
};
var response = isoReader.Transmit(apdu);
Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}", response.SW1, response.SW2);
// ..
}
}
Read reader attributes
using (var ctx = ContextFactory.Instance.Establish(SCardScope.System)) {
using (var reader = ctx.ConnectReader("OMNIKEY CardMan 5x21 0", SCardShareMode.Shared, SCardProtocol.Any)) {
var cardAtr = reader.GetAttrib(SCardAttribute.AtrString);
Console.WriteLine("ATR: {0}", BitConverter.ToString(cardAtr));
Console.ReadKey();
}
}
Monitor reader events
var monitorFactory = MonitorFactory.Instance;
var monitor = monitorFactory.Create(SCardScope.System);
// connect events here..
monitor.StatusChanged += (sender, args) =>
Console.WriteLine($"New state: {args.NewState}");
monitor.Start("OMNIKEY CardMan 5x21-CL 0");
Console.ReadKey(); // Press any key to exit
monitor.Cancel();
monitor.Dispose();
More example code
Checkout the Examples directory.
Build from source
Required software
Build tools
Fake, Please run:
dotnet tool install fake-cli -g
to install
fake
as global tool. On Linux you may have to add the following lines into your .profile or .bashrc file:if [ -d "$HOME/.dotnet/tools" ] ; then PATH="$HOME/.dotnet/tools:$PATH" fi
pcsc-sharp uses the FAKE DSL for build tasks. To build the solution, run fake run build.fsx
.
Build instructions for Raspberry Pi
Compile with
dotnet publish -r linux-arm
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages (10)
Showing the top 5 NuGet packages that depend on PCSC:
Package | Downloads |
---|---|
PCSC.Iso7816
Implements partial ISO7816 support (APDU etc.) for PCSC. |
|
ArsMagna.Rfid
ArsMagna RFID services |
|
PCSC.Reactive
Reactive extensions for PC/SC. Observables from smart card monitor events. |
|
MRTD.NET
MRTD.NET |
|
ThaiNationalIDCard
Thailand National ID Smart card Helper |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on PCSC:
Repository | Stars |
---|---|
chakphanu/ThaiNationalIDCard
ThaiNational IDCard(Smartcard) helper
|
Version | Downloads | Last updated |
---|---|---|
6.1.3 | 1,666 | 12/19/2022 |
6.1.2 | 1,211 | 12/1/2022 |
6.1.1 | 296 | 12/1/2022 |
6.1.0 | 323 | 11/30/2022 |
6.0.0 | 19,095 | 3/9/2022 |
5.1.0 | 12,314 | 12/6/2021 |
5.0.0 | 89,047 | 12/7/2019 |
4.2.0 | 19,894 | 7/3/2019 |
4.1.1 | 2,353 | 5/26/2019 |
4.1.0 | 871 | 5/26/2019 |
4.0.3 | 3,970 | 2/18/2019 |
4.0.2 | 3,166 | 2/4/2019 |
4.0.1 | 7,506 | 9/24/2018 |
4.0.0 | 55,621 | 2/25/2018 |
3.8.0 | 21,045 | 10/8/2017 |
3.7.0 | 8,362 | 6/22/2017 |
3.6.1 | 9,584 | 4/12/2017 |
3.6.0 | 11,556 | 11/18/2016 |
3.5.3 | 2,066 | 11/3/2016 |
3.5.2 | 1,178 | 11/2/2016 |
3.5.1 | 16,079 | 5/24/2016 |
3.5.0 | 6,291 | 4/19/2016 |
3.4.1 | 2,083 | 3/5/2016 |
3.4.0 | 1,208 | 2/11/2016 |
3.3.1 | 1,622 | 1/21/2016 |
3.3.0 | 2,205 | 10/28/2015 |
3.2.0 | 2,133 | 7/31/2015 |
3.1.0 | 1,443 | 7/12/2013 |
3.0.0 | 1,375 | 5/20/2013 |