.NET
  Home arrow .NET arrow Page 2 - Smart Cards in .NET, Part 3
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Smart Cards in .NET, Part 3
By: Digvijay Chauhan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 36
    2004-11-15

    Table of Contents:
  • Smart Cards in .NET, Part 3
  • Two classes to rule them
  • Using the Library: The Demo
  • Smart card resource manager
  • CardCommand and SelectSmart
  • Source code for the library

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Smart Cards in .NET, Part 3 - Two classes to rule them


    (Page 2 of 6 )

    Working along the following lines, we may have two classes: one for resource manager, and another for reader functions, so that they encapsulate the Resource Manager and the Smart Card reader itself. A few helper classes that let you deal with the data exchanged with the card and the commands that you pass to the card can also be added, as I’ve done in our library.

    As with any other component, library errors and exceptions are something that we can’t ignore, so I’ve also included a SCardException class, which itself derives from ApplicationException class and provides encapsulation on the response code from the card. Added to that, you can raise an exception based on a Win32 error code that you usually get from the native WinSCard API. Also, an internal sealed class WinSCard provides the primitives for P/Invoking the native APIs, and you may have a look at it in the attached code for this article.

    So at this point our classes look like this:

    Figure 1  Classes for our Library

    The class SCResMgr contains the code to manage Contexts with the Smart Card Resource Manager and lets us use it with an instance of SCReader. Indeed, you can’t create a SCReader without a SCResMgr!

    Let’s have a look at the members of the SCResMgr:

    public void Cancel()

    Is a member Equivalent to native SCardCancel() and the code looks something like the code below:

    // Tracking Functions
    public void Cancel()
    {
     if (this.m_hContext == IntPtr.Zero)
     {
      throw new InvalidOperationException();
     }
     uint nErrCode = WinSCard.SCardCancel(this.m_hContext);
     if (nErrCode != 0)
     {
      SCardException.RaiseWin32ResponseCode(nErrCode);
     }
    }

    public bool EstablishContext(SCardContextScope nScope)

    Is equivalent to SCardEstablishContext and uses encapsulated Context handle. Note how the Context Scope constants in the platform SDK have been grouped in an enum to facilitate their use.

    public bool GetStatusChange(int nTimeout, SCStateInfo[] vaSCStates)

    Is similar to the SCardGetStatusChange API and this contains three overloads to make its use easier as compared to the native API.

    These three examples above will be able to explain to you the style adopted for the library. You will find more details as you get to use the library. For a quick look refer to figure 2, which shows all the members of the SCResMgr library. For a more detailed view, look at the source code listing for the SCResMgr API later in the article.

    Figure 2  SCResMgr Class View

    Next you may see the Class created for SCReader that encapsulates a Smartcard Reader in figure 3.

    Figure 3  The SCReaderClassView

    Finally you can see all the classes and Enumerations in the figure below. This code, being a part of a commercial product, can’t be released publicly, but a free compiled version is certainly a strong candidate for release with this article.

    Figure 4  The completed library

    More .NET Articles
    More By Digvijay Chauhan


       · Hi,I'm Digvijay .... the author of this article.Please do let me know how you...
       · Article looks very helpful. Will start looking and using them to suit our...
       · Hi,This is basically for managed code that is accessible from all .NET...
       · Hi, I am trying to access memory card (sle4428), as far as I know I need to use...
       · Hi,I jut provided the code to give an overview of how to go about it that is why...
       · Hi,This looks VERY useful. When can we expect the commercial library? I want to...
       · Hi ,You can expect that in March 2005 only.Regards,Digvijay
       · I am attempting to use the code provided and running into a little problem. I have...
       · Hi.In yur first article, what is the SCardLIB 1.0 component?In my Visual Basic...
       · Hi there,This code is quite interesting to me. It works great. Nice work. But I...
       · Hi, the library works fine in some readers, but with the next...
       · hi siri am Ramzy Ebiedi am using C#, ASP.Net VS2005i did test your...
       · Dear DigVijay,I would appreciate if you help me out....I'm developing an...
       · hi,I had this problem too, I tested my application with different cards,some...
       · Hi all,I'm very grateful to Mr. Digvijay for his article. Very interesting.I'm a...
       · Hi all, I'm very grateful to Mr. Digvijay for his article. Very interesting. I'm...
       · How to use your library to write a string like "hello word" and to read from the...
     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek