Click here to Skip to main content
15,896,489 members
Articles / Programming Languages / C#

Using Smart cards with a Windows Store App

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
10 Feb 2013CPOL8 min read 32.2K   1.7K   18  
This article describes a solution to access API and resources that are not available with WinRT.
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18033
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// 
// This code was auto-generated by Microsoft.VisualStudio.ServiceReference.Platforms, version 11.0.50727.1
// 
namespace Core.WinRTSCard.SCardService {
    using System.Runtime.Serialization;
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="SHARE", Namespace="http://schemas.datacontract.org/2004/07/GemCard")]
    internal enum SHARE : int {
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Exclusive = 1,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Shared = 2,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Direct = 3,
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="PROTOCOL", Namespace="http://schemas.datacontract.org/2004/07/GemCard")]
    internal enum PROTOCOL : int {
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Undefined = 0,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        T0 = 1,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        T1 = 2,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Raw = 65536,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Default = -2147483648,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        T0orT1 = 3,
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="DISCONNECT", Namespace="http://schemas.datacontract.org/2004/07/GemCard")]
    internal enum DISCONNECT : int {
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Leave = 0,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Reset = 1,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Unpower = 2,
        
        [System.Runtime.Serialization.EnumMemberAttribute()]
        Eject = 3,
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="APDUCommand", Namespace="http://schemas.datacontract.org/2004/07/GemCard.Service")]
    internal partial class APDUCommand : object, System.ComponentModel.INotifyPropertyChanged {
        
        private byte ClassField;
        
        private byte[] DataField;
        
        private byte InsField;
        
        private byte LeField;
        
        private byte P1Field;
        
        private byte P2Field;
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte Class {
            get {
                return this.ClassField;
            }
            set {
                if ((this.ClassField.Equals(value) != true)) {
                    this.ClassField = value;
                    this.RaisePropertyChanged("Class");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte[] Data {
            get {
                return this.DataField;
            }
            set {
                if ((object.ReferenceEquals(this.DataField, value) != true)) {
                    this.DataField = value;
                    this.RaisePropertyChanged("Data");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte Ins {
            get {
                return this.InsField;
            }
            set {
                if ((this.InsField.Equals(value) != true)) {
                    this.InsField = value;
                    this.RaisePropertyChanged("Ins");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte Le {
            get {
                return this.LeField;
            }
            set {
                if ((this.LeField.Equals(value) != true)) {
                    this.LeField = value;
                    this.RaisePropertyChanged("Le");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte P1 {
            get {
                return this.P1Field;
            }
            set {
                if ((this.P1Field.Equals(value) != true)) {
                    this.P1Field = value;
                    this.RaisePropertyChanged("P1");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte P2 {
            get {
                return this.P2Field;
            }
            set {
                if ((this.P2Field.Equals(value) != true)) {
                    this.P2Field = value;
                    this.RaisePropertyChanged("P2");
                }
            }
        }
        
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        
        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="APDUResponse", Namespace="http://schemas.datacontract.org/2004/07/GemCard.Service")]
    internal partial class APDUResponse : object, System.ComponentModel.INotifyPropertyChanged {
        
        private byte[] DataField;
        
        private byte SW1Field;
        
        private byte SW2Field;
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte[] Data {
            get {
                return this.DataField;
            }
            set {
                if ((object.ReferenceEquals(this.DataField, value) != true)) {
                    this.DataField = value;
                    this.RaisePropertyChanged("Data");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte SW1 {
            get {
                return this.SW1Field;
            }
            set {
                if ((this.SW1Field.Equals(value) != true)) {
                    this.SW1Field = value;
                    this.RaisePropertyChanged("SW1");
                }
            }
        }
        
        [System.Runtime.Serialization.DataMemberAttribute()]
        internal byte SW2 {
            get {
                return this.SW2Field;
            }
            set {
                if ((this.SW2Field.Equals(value) != true)) {
                    this.SW2Field = value;
                    this.RaisePropertyChanged("SW2");
                }
            }
        }
        
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        
        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="SCardService.IRemoteCard")]
    internal interface IRemoteCard {
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IRemoteCard/ListReaders", ReplyAction="http://tempuri.org/IRemoteCard/ListReadersResponse")]
        System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<string>> ListReadersAsync();
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IRemoteCard/Connect", ReplyAction="http://tempuri.org/IRemoteCard/ConnectResponse")]
        System.Threading.Tasks.Task ConnectAsync(string Reader, Core.WinRTSCard.SCardService.SHARE ShareMode, Core.WinRTSCard.SCardService.PROTOCOL PreferredProtocols);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IRemoteCard/Disconnect", ReplyAction="http://tempuri.org/IRemoteCard/DisconnectResponse")]
        System.Threading.Tasks.Task DisconnectAsync(Core.WinRTSCard.SCardService.DISCONNECT Disposition);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IRemoteCard/Transmit", ReplyAction="http://tempuri.org/IRemoteCard/TransmitResponse")]
        System.Threading.Tasks.Task<Core.WinRTSCard.SCardService.APDUResponse> TransmitAsync(Core.WinRTSCard.SCardService.APDUCommand ApduCmd);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IRemoteCard/BeginTransaction", ReplyAction="http://tempuri.org/IRemoteCard/BeginTransactionResponse")]
        System.Threading.Tasks.Task BeginTransactionAsync();
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IRemoteCard/EndTransaction", ReplyAction="http://tempuri.org/IRemoteCard/EndTransactionResponse")]
        System.Threading.Tasks.Task EndTransactionAsync(Core.WinRTSCard.SCardService.DISCONNECT Disposition);
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IRemoteCard/GetAttribute", ReplyAction="http://tempuri.org/IRemoteCard/GetAttributeResponse")]
        System.Threading.Tasks.Task<byte[]> GetAttributeAsync(uint AttribId);
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    internal interface IRemoteCardChannel : Core.WinRTSCard.SCardService.IRemoteCard, System.ServiceModel.IClientChannel {
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    internal partial class RemoteCardClient : System.ServiceModel.ClientBase<Core.WinRTSCard.SCardService.IRemoteCard>, Core.WinRTSCard.SCardService.IRemoteCard {
        
        /// <summary>
        /// Implement this partial method to configure the service endpoint.
        /// </summary>
        /// <param name="serviceEndpoint">The endpoint to configure</param>
        /// <param name="clientCredentials">The client credentials</param>
        static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
        
        public RemoteCardClient() : 
                base(RemoteCardClient.GetDefaultBinding(), RemoteCardClient.GetDefaultEndpointAddress()) {
            this.Endpoint.Name = EndpointConfiguration.NetTcpBinding_IRemoteCard.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }
        
        public RemoteCardClient(EndpointConfiguration endpointConfiguration) : 
                base(RemoteCardClient.GetBindingForEndpoint(endpointConfiguration), RemoteCardClient.GetEndpointAddress(endpointConfiguration)) {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }
        
        public RemoteCardClient(EndpointConfiguration endpointConfiguration, string remoteAddress) : 
                base(RemoteCardClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress)) {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }
        
        public RemoteCardClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(RemoteCardClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress) {
            this.Endpoint.Name = endpointConfiguration.ToString();
            ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
        }
        
        public RemoteCardClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress) {
        }
        
        public System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<string>> ListReadersAsync() {
            return base.Channel.ListReadersAsync();
        }
        
        public System.Threading.Tasks.Task ConnectAsync(string Reader, Core.WinRTSCard.SCardService.SHARE ShareMode, Core.WinRTSCard.SCardService.PROTOCOL PreferredProtocols) {
            return base.Channel.ConnectAsync(Reader, ShareMode, PreferredProtocols);
        }
        
        public System.Threading.Tasks.Task DisconnectAsync(Core.WinRTSCard.SCardService.DISCONNECT Disposition) {
            return base.Channel.DisconnectAsync(Disposition);
        }
        
        public System.Threading.Tasks.Task<Core.WinRTSCard.SCardService.APDUResponse> TransmitAsync(Core.WinRTSCard.SCardService.APDUCommand ApduCmd) {
            return base.Channel.TransmitAsync(ApduCmd);
        }
        
        public System.Threading.Tasks.Task BeginTransactionAsync() {
            return base.Channel.BeginTransactionAsync();
        }
        
        public System.Threading.Tasks.Task EndTransactionAsync(Core.WinRTSCard.SCardService.DISCONNECT Disposition) {
            return base.Channel.EndTransactionAsync(Disposition);
        }
        
        public System.Threading.Tasks.Task<byte[]> GetAttributeAsync(uint AttribId) {
            return base.Channel.GetAttributeAsync(AttribId);
        }
        
        public virtual System.Threading.Tasks.Task OpenAsync() {
            return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
        }
        
        public virtual System.Threading.Tasks.Task CloseAsync() {
            return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
        }
        
        private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration) {
            if ((endpointConfiguration == EndpointConfiguration.NetTcpBinding_IRemoteCard)) {
                System.ServiceModel.NetTcpBinding result = new System.ServiceModel.NetTcpBinding();
                result.MaxBufferSize = int.MaxValue;
                result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                result.MaxReceivedMessageSize = int.MaxValue;
                result.Security.Mode = System.ServiceModel.SecurityMode.None;
                return result;
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }
        
        private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration) {
            if ((endpointConfiguration == EndpointConfiguration.NetTcpBinding_IRemoteCard)) {
                return new System.ServiceModel.EndpointAddress("net.tcp://localhost:8001/SCardService");
            }
            throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
        }
        
        private static System.ServiceModel.Channels.Binding GetDefaultBinding() {
            return RemoteCardClient.GetBindingForEndpoint(EndpointConfiguration.NetTcpBinding_IRemoteCard);
        }
        
        private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress() {
            return RemoteCardClient.GetEndpointAddress(EndpointConfiguration.NetTcpBinding_IRemoteCard);
        }
        
        public enum EndpointConfiguration {
            
            NetTcpBinding_IRemoteCard,
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Connect In Private
Singapore Singapore
Software Architect, COM, .NET and Smartcard based security specialist.

I've been working in the software industry since I graduated in Electrical and Electronics Engineering. I chose software because I preferred digital to analog.

I started to program with 6802 machine code and evolved to the current .NET technologies... that was a long way.

For more than 20 years I have always worked in technical positions as I simply like to get my hands dirty and crack my brain when things don't go right!

After 12 years in the smart card industry I can claim a strong knowledge in security solutions based on those really small computers!
I've been back into business to design the licensing system for the enterprise solution for Consistel using a .NET smart card (yes they can run .NET CLR!)

I'm currently designing a micro-payment solution using the NXP DESFire EV1 with the ACSO6 SAM of ACS. I can then add a full proficient expertise on those systems and NFC payments.
This technology being under strict NDA by NXP I cannot publish any related article about it, however I can provide professional consulting for it.

You can contact me for professional matter by using the forum or via my LinkedIn profile.

Comments and Discussions