Skip to content

Repository files navigation

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-sharpis 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-sharpdoes 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
    • 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.

varcontextFactory=ContextFactory.Instance;using(varcontext=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.

varcontextFactory=ContextFactory.Instance;using(varcontext=contextFactory.Establish(SCardScope.System)){Console.WriteLine("Currently connected readers: ");varreaderNames=context.GetReaders();foreach(varreaderNameinreaderNames){Console.WriteLine("\t"+readerName);}}

Send ISO7816 APDUs

varcontextFactory=ContextFactory.Instance;using(varctx=contextFactory.Establish(SCardScope.System)){using(varisoReader=newIsoReader(ctx,"ACME Smartcard reader",SCardShareMode.Shared,SCardProtocol.Any,false)){varapdu=newCommandApdu(IsoCase.Case2Short,isoReader.ActiveProtocol){CLA=0x00,// ClassInstruction=InstructionCode.GetChallenge,P1=0x00,// Parameter 1P2=0x00,// Parameter 2Le=0x08// Expected length of the returned data};varresponse=isoReader.Transmit(apdu);Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}",response.SW1,response.SW2);// ..}}

Read reader attributes

using(varctx=ContextFactory.Instance.Establish(SCardScope.System)){using(varreader=ctx.ConnectReader("OMNIKEY CardMan 5x21 0",SCardShareMode.Shared,SCardProtocol.Any)){varcardAtr=reader.GetAttrib(SCardAttribute.AtrString);Console.WriteLine("ATR: {0}",BitConverter.ToString(cardAtr));Console.ReadKey();}}

Monitor reader events

varmonitorFactory=MonitorFactory.Instance;varmonitor=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 exitmonitor.Cancel();monitor.Dispose();

More example code

Checkout the Examples directory.

Build from source

Build status

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

About

PC/SC wrapper classes for .NET, written in C#

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages