ListCardsInFileRequest.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / infocard / Service / managed / Microsoft / InfoCards / ListCardsInFileRequest.cs / 1 / ListCardsInFileRequest.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.InfoCards
{ 
    using System;
    using System.Collections.Generic; 
    using System.IO; 
    using System.Xml;
    using System.Security.Cryptography; 
    using System.Xml.Schema;
    using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;

    // 
    // Open the file using the password and retrieves all the cards
    // in an import file. 
    // 
    class ListCardsInFileRequest :UIAgentRequest
    { 
        InfoCardPolicy m_policy;
        RoamingStoreFile m_roamingFile;
        string m_filename;
        string m_passphrase; 

 
        public ListCardsInFileRequest( IntPtr rpcHandle, Stream inArgs, Stream outArgs, ClientUIRequest parent ) 
            : base( rpcHandle, inArgs, outArgs, parent )
        { 
            m_roamingFile = new RoamingStoreFile();
        }

 
        protected override void OnInitializeAsSystem()
        { 
            base.OnInitializeAsSystem(); 
            m_policy = GetPolicy();
        } 

        //
        // Summary
        //  Read the marshalled arguments 
        //
        protected override void OnMarshalInArgs() 
        { 
            BinaryReader reader = new InfoCardBinaryReader( InArgs, System.Text.Encoding.Unicode );
            m_filename = Utility.DeserializeString( reader ); 
            m_passphrase = Utility.DeserializeString( reader );
        }

        // 
        // Summary
        //  Read the list of cards from the store 
        // 
        protected override void OnProcess()
        { 
            //
            // clean up any existing card data.
            //
            Cleanup(false,true); 
            try
            { 
                try 
                {
 
                    try
                    {
                        using ( FileStream fileStream = new FileStream( m_filename, FileMode.Open, FileAccess.Read, FileShare.Read ) )
                        { 
                            //
                            // Use a stream that validates the xml against internally stored schemas. 
                            // 
                            XmlReaderSettings settings = InfoCardSchemas.CreateDefaultReaderSettings();
                            settings.IgnoreWhitespace = false; 
                            using ( XmlReader reader = InfoCardSchemas.CreateReader( fileStream, settings ) )
                            {
                                IDT.TraceDebug( " Roaming: Decrypting the roaming store" );
 
                                m_roamingFile.ReadFrom( m_passphrase, reader );
 
 
                                //
                                // Store a pointer to the original content from the file. 
                                //  this is the content that contains the private key info
                                //  for each infocard.  This is required as persist time.
                                //
                                ParentRequest.SetContext( m_roamingFile ); 
                            }
                        } 
                    } 
                    catch( ImportException )
                    { 
                        throw;
                    }
                    catch ( CryptographicException e )
                    { 
                        throw IDT.ThrowHelperError(
                            new PasswordValidationException( SR.GetString( SR.InvalidImportFile ), e ) ); 
                    } 
                    catch ( XmlSchemaValidationException e )
                    { 
                        throw IDT.ThrowHelperError(
                           new ImportException( SR.GetString( SR.SchemaValidationFailed ), e ) );
                    }
                    catch ( UnauthorizedAccessException e ) 
                    {
 
                        throw IDT.ThrowHelperError( 
                            new ImportException( SR.GetString( SR.ImportInaccesibleFile ), e ) );
                    } 
                    catch ( IOException e )
                    {
                        throw IDT.ThrowHelperError(
                            new ImportException( SR.GetString( SR.InvalidImportFile ), e ) ); 
                    }
                    catch ( XmlException e ) 
                    { 
                        throw IDT.ThrowHelperError(
                            new ImportException( SR.GetString( SR.InvalidImportFile ), e ) ); 
                    }
                }
                catch ( ImportException ie )
                { 
                    //
                    // clear the cards, pwd will be cleared below. 
                    // 
                    Cleanup(false, true);
 

                    //
                    // Translate ImportException to ImportStoreException since this is a .crds file
                    // 

                    // 
                    // Using ie.Message as opposed to SR.GetString( SR.InvalidStoreImportFile ) 
                    // for more accurate error information.
                    // 
                    throw IDT.ThrowHelperError(
                            new ImportStoreException(
                                ie.Message
                                ) ); 
                }
            } 
            finally 
            {
                // 
                // clear the pwd not the cards
                //
                Cleanup(true, false);
            } 

        } 
 
        //
        // Summary 
        //   Write the cards to the stream to be returned
        //
        protected override void OnMarshalOutArgs()
        { 
            Stream stream = OutArgs;
 
            BinaryWriter writer = new BinaryWriter( stream ); 

            StoreConnection connection = StoreConnection.GetConnection(); 
            try
            {
                if( null != m_roamingFile && 0 != m_roamingFile.Cards.Count )
                { 
                    IDT.TraceDebug( " Roaming: Returning {0} cards to the UI", m_roamingFile.Cards.Count );
                    // 
                    // write the count of the array. 
                    //
                    try 
                    {
                        writer.Write( m_roamingFile.Cards.Count );

                        for( int i = 0; i < m_roamingFile.Cards.Count; i++ ) 
                        {
                            // 
                            // Flush the writer to ensure that all data 
                            //  is committed to the base stream before
                            //  we pass the base stream into the card. 
                            //
                            writer.Flush();
                            m_roamingFile.Cards[ i ].AgentSerialize(
                                stream, 
                                ( ParentRequest is GetTokenRequest ),
                                m_policy, 
                                connection, 
                                new System.Globalization.CultureInfo( ParentRequest.UserLanguage ) );
 
                        }
                    }
                    catch( Exception e )
                    { 
                        Cleanup( true, true );
                        if( IDT.IsFatal( e ) ) 
                        { 
                            throw;
                        } 
                        throw IDT.ThrowHelperError(
                            new ImportStoreException( SR.GetString( SR.InvalidImportFile ), e ) );
                    }
                } 
                else
                { 
                    // 
                    // write the count of the array as 0.
                    // 
                    writer.Write( 0 );
                }
            }
            finally 
            {
                connection.Close(); 
            } 
        }
 



 
        //
        // Summary: Clear out sensitive data present in an infocard 
        // 
        private void Cleanup( bool clearPwd, bool clearRoamingFile )
        { 
            if( clearRoamingFile )
            {
                if( null != m_roamingFile )
                { 
                    m_roamingFile.Clear();
                } 
                if( null != ParentRequest ) 
                {
                    ParentRequest.ClearContext(); 
                }
            }

            if( clearPwd ) 
            {
                m_passphrase = null; 
            } 
        }
 
    }

}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK