Skip to content
PC/SC wrapper classes for .NET, written in C#
C# Other
  1. C# 99.7%
  2. Other 0.3%
Branch: master
Clone or download
danm-de Update README.md
pcsc-sharp does not contain any drive driver.
Latest commit f809f4a Oct 6, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Examples.Wpf Moved all Unit-Tests and remaining example projects to .Net Core May 25, 2019
Examples Deleted old Wpf example project May 25, 2019
Tests SCardReaderStateTest, Arithmetric exception May 25, 2019
src Update package description Aug 2, 2019
.editorconfig New coding styles in editorconfig May 24, 2019
.gitignore Update paket / bootstrapper to newest version. May 8, 2019
AUTHORS New version 4.0.2 - added MacOS support by @pedromrpinto Feb 4, 2019
COPYING New version 4.0.2 - added MacOS support by @pedromrpinto Feb 4, 2019
Changelog New version 4.2.0 Jul 3, 2019
README.md Update README.md Oct 6, 2019
appveyor.yml AppVeyor configuration changed May 25, 2019
build.cmd Introduce FAKE 5 as new build system May 25, 2019
build.fsx Fix build file (build.fsx) Jul 3, 2019
build.sh Added executable bit to build.sh and fake.sh May 25, 2019
fake.cmd Introduce FAKE 5 as new build system May 25, 2019
fake.sh Added executable bit to build.sh and fake.sh May 25, 2019
paket.dependencies Introduce FAKE 5 as new build system May 25, 2019
paket.lock Introduce FAKE 5 as new build system May 25, 2019
pcsc-sharp.PublicKey Added signing key (github issue #90 and pull request #88) May 25, 2019
pcsc-sharp.png Changed pcsc-sharp libraries to .netstandard 2.0 Feb 18, 2018
pcsc-sharp.sln Added signing key (github issue #90 and pull request #88) May 25, 2019
pcsc-sharp.sln.DotSettings Update paket / bootstrapper to newest version. May 8, 2019
pcsc-sharp.snk Added signing key (github issue #90 and pull request #88) May 25, 2019

README.md

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.

You can find PC/SC specific documentation here:

Supported Operating systems

  • Windows (winscard.dll)

    • Windows 10 64-bit Professional (.Net Core 2.2 confirmed)
    • Windows 10 32-bit Professional
    • Windows 7 64-bit
    • Windows 7 32-bit
  • Linux (PC/SC lite)

    • Ubuntu Linux 64-bit (.Net Core 2.2 confirmed)
    • Ubuntu Linux 32-bit
  • MacOS X (.Net Core 2.2 confirmed)

  • Raspberry Pi / Linux ARM

    • linux-arm (.Net Core 2.2 confirmed)

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

Frameworks

  • .Net Core 2.2 SDK
  • .Net Core 3.0 Preview (WPF examples)

Build tools

  • Fake (https://fake.build/fake-dotnetcore.html), 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 great FAKE DSL for build tasks. To build the solution, simply start the build.cmd on Windows or the build.sh shell script on Unix.

Build instructions for Raspberry Pi

Compile with

dotnet publish -r linux-arm
You can’t perform that action at this time.