PCSC 7.0.0

dotnet add package PCSC --version 7.0.0                
NuGet\Install-Package PCSC -Version 7.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PCSC" Version="7.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PCSC --version 7.0.0                
#r "nuget: PCSC, 7.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PCSC as a Cake Addin
#addin nuget:?package=PCSC&version=7.0.0

// Install PCSC as a Cake Tool
#tool nuget:?package=PCSC&version=7.0.0                

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:

Supported Operating systems

  • Windows (winscard.dll)

    • Windows 11 64-bit
    • Windows 10 64-bit Professional
    • Windows 10 32-bit Professional
  • 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

Build status

Required software

Build tools

  • .Net 8.0 SDKs (Multi target build)

Compile with

dotnet build -c Release

Build instructions for Raspberry Pi

Compile with

dotnet publish -r linux-arm
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on PCSC:

Package Downloads
PCSC.Iso7816

Implements partial ISO7816 support (APDU etc.) for PCSC.

PCSC.Reactive

Reactive extensions for PC/SC. Observables from smart card monitor events.

ArsMagna.Rfid

ArsMagna RFID services

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
7.0.0 12,269 8/23/2024
6.2.0 54,447 12/30/2023
6.1.3 58,668 12/19/2022
6.1.2 5,439 12/1/2022
6.1.1 1,200 12/1/2022
6.1.0 1,166 11/30/2022
6.0.0 40,215 3/9/2022
5.1.0 31,718 12/6/2021
5.0.0 149,648 12/7/2019
4.2.0 26,256 7/3/2019
4.1.1 3,257 5/26/2019
4.1.0 1,522 5/26/2019
4.0.3 4,849 2/18/2019
4.0.2 3,907 2/4/2019
4.0.1 8,699 9/24/2018
4.0.0 83,249 2/25/2018
3.8.0 55,984 10/8/2017
3.7.0 10,619 6/22/2017
3.6.1 16,170 4/12/2017
3.6.0 13,006 11/18/2016
3.5.3 2,267 11/3/2016
3.5.2 1,343 11/2/2016
3.5.1 18,776 5/24/2016
3.5.0 6,870 4/19/2016
3.4.1 2,323 3/5/2016
3.4.0 1,398 2/11/2016
3.3.1 1,892 1/21/2016
3.3.0 2,523 10/28/2015
3.2.0 2,642 7/31/2015
3.1.0 1,644 7/12/2013
3.0.0 1,644 5/20/2013

Breaking change introduced in 7.0.0: The *IsoReader.MaxReceiveSize* default value changed from 128 to 255. See [pull request #118](https://github.com/danm-de/pcsc-sharp/pull/118) for more information.