InfoCardClaimCollection.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 / InfoCardClaimCollection.cs / 1 / InfoCardClaimCollection.cs

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

    // 
    // Summary
    // This class contains a map of all of the claim values associated with a specific InfoCard 
    // It supports persistence to the store as a single object. The dictionary is keyed by 
    // the claim type URI as a string.
    // 
    // Remarks
    // The parentId index is populated with the URI of the InfoCard associated with this set of PII.
    //
    internal class InfoCardClaimCollection : Dictionary 
    {
        const Int32 InvalidRow = 0; 
 
        Uri m_infoCardId;
        Int32 m_rowId = InvalidRow; 

        //
        // Summary
        // Creates an uninitialized instance of the InfoCardClaimCollection. 
        //
        // Remarks 
        // This behavior is not supported and is thus declared as private. 
        //
        private InfoCardClaimCollection() 
        {
        }

        // 
        // Summary
        // Creates a new instance of the InfoCardClaimCollection associated with the 
        // InfoCard whose Id is specified. 
        //
        // Parameters 
        // infoCardId   - Id of the owning InfoCard
        //
        public InfoCardClaimCollection( Uri infoCardId )
            : this() 
        {
            if( null == infoCardId ) 
            { 
                throw IDT.ThrowHelperArgumentNull( "infoCardId" );
            } 

            m_infoCardId = infoCardId;
        }
 
        //
        // Summary 
        // Adds or updates the collection with the claim information specified. 
        //
        // Parameters 
        // claim    - InfoCard claim to be added or updated to the collection. This
        //           is a shallow copy.
        //
        public void Add( InfoCardClaim claim ) 
        {
            if( null == claim ) 
            { 
                throw IDT.ThrowHelperArgumentNull( "claim" );
            } 

            this[ claim.Id ] = claim;
        }
 
        //
        // Summary 
        // Retrieves the claims information from the store and populates the collection 
        // with it. The InfoCard Id specified in the constructor is used as the parent key.
        // 
        // Parameters
        // con  - Connection to the store to be used to retrieve the claims collection object.
        //
        public void Get( StoreConnection con ) 
        {
            if( null == con ) 
            { 
                throw IDT.ThrowHelperArgumentNull( "con" );
            } 

            IDT.TraceDebug( "Retrieving the claims for infocard({0})", m_infoCardId );

            // 
            // Find the row for the object from the database, returns null if not exists
            // 
            DataRow row = TryGetRow( con, QueryDetails.FullRow ); 

            // 
            // A claims object does not necessarily exist for this infocard
            //
            if( null != row )
            { 
                //
                // Populate the claims from the byte array 
                // 
                Deserialize( new MemoryStream( row.GetDataField() ) );
 
                //
                // Update the row id from the database
                //
                m_rowId = row.LocalId; 
            }
        } 
 
        //
        // Summary 
        // Writes all of the claims information into a binary stream in an unordered sequence.
        //
        // Remarks
        // The layout in the stream is as follows: 
        //      Int32   - Count of claims that follows
        //      Claim[] - [See InfoCardClaim for serialization format] 
        // 
        // Parameters
        // stream     - Stream that will receive the byte stream of each claim. 
        // selfIssued - Specifies whether the card is self-issued.
        //
        public void Serialize( Stream stream, bool selfIssued )
        { 
            //
            // Setup a BinaryWriter to serialize the bytes of each member to the provided stream 
            // 
            BinaryWriter writer = new BinaryWriter( stream, System.Text.Encoding.Unicode );
 
            int countToWrite = ( Int32 ) this.Count;

            if( selfIssued && this.ContainsKey( InfoCardConstants.PPIDClaimsUri ) )
            { 
                //
                // Will not write PPID for self issued cards 
                // 
                --countToWrite;
            } 

            writer.Write( ( Int32 ) countToWrite );

            foreach( KeyValuePair pair in this ) 
            {
                // 
                // For self issued cards, write everthing except the PPID claim 
                //
                if( selfIssued ) 
                {
                    if( InfoCardConstants.PPIDClaimsUri != pair.Key )
                    {
                        pair.Value.Serialize( writer ); 
                    }
                } 
                else 
                {
                    pair.Value.Serialize( writer ); 
                }
            }
        }
 
        //
        // Summary 
        // Writes all of the claims information into a binary stream in an unordered sequence FOR the agent 
        //
        // Remarks 
        // The layout in the stream is as follows:
        //      Int32   - Count of claims that follows
        //      Claim[] - [See InfoCardClaim for serialization format]
        // 
        // Parameters
        // stream   - Stream that will receive the byte stream of each claim. 
        // 
        public void AgentSerialize( Stream stream )
        { 
            //
            // Setup a BinaryWriter to serialize the bytes of each member to the provided stream
            //
            BinaryWriter writer = new BinaryWriter( stream, System.Text.Encoding.Unicode ); 
            writer.Write( ( Int32 ) this.Count );
            foreach( KeyValuePair pair in this ) 
            { 
                pair.Value.Serialize( writer );
            } 
        }


 

        // 
        // Summary 
        // Reads a list of serialized claims from the provided stream into the receiving collection.
        // 
        // Remarks
        // The layout int the stream is as follows:
        //      Int32   - Count of claims that follows
        //      Claim[] - [See InfoCardClaim for serialization format] 
        //
        // Parameters 
        // stream   - Stream that will provide the byte stream of claims. 
        //
        // Remarks: 
        // The incoming stream (if from the agent) will include PPID.
        // The incoming stream (if from store) will NOT include PPID.
        //
        public void Deserialize( Stream stream ) 
        {
            // 
            // Populate each member from the stream 
            //
            BinaryReader reader = new InfoCardBinaryReader( stream, System.Text.Encoding.Unicode ); 
            //
            // Get the count of the number of claims in this object
            //
            Int32 n = reader.ReadInt32(); 
            IDT.TraceDebug( "Found {0} claims for this infocard id({1}) in the store.",
                            n, m_infoCardId ); 
 
            for( Int32 i = 0; i < n; i++ )
            { 
                InfoCardClaim claim = new InfoCardClaim();
                claim.DeSerialize( reader );

                this[ claim.Id ] = claim; 
            }
        } 
 
        //
        // Summary 
        // Writes a serialized collection of InfoCardClaim objects to the roaming instance of
        // store.
        //
        // Remarks 
        // The parentId index field is set to the Id of the associated InfoCard.
        // 
        // Parameters 
        // con        - Connection to the store to be used to store the claims collection object.
        // selfIssued - Specifies whether the card is self-issued. 
        //
        public void Save( StoreConnection con, bool selfIssued )
        {
            if( null == con ) 
            {
                throw IDT.ThrowHelperArgumentNull( "con" ); 
            } 

            // 
            // Write the claims collection to the store associated
            // this collection with the infocard id specified.
            //
            IDT.TraceDebug( "Saving the claims collection..." ); 
            IDT.TraceDebug( ToString() );
 
            // 
            // Try and get the database header information to
            // see if this is an insert or update. 
            //
            // Note: The datafield is not part of the projection
            // in order to avoid unnecessary decryption.
            // 
            DataRow row = TryGetRow( con, QueryDetails.FullHeader );
 
            if( null == row ) 
            {
                row = new DataRow(); 
                row.ObjectType = ( Int32 )StorableObjectType.InfoCardClaims;
                row.GlobalId = Guid.NewGuid();
            }
 
            //
            // Populate the parentId index field 
            // 
            row.SetIndexValue( SecondaryIndexDefinition.ParentIdIndex,
                 GlobalId.DeriveFrom( m_infoCardId.ToString() ) ); 

            //
            // Populate the data object
            // 
            MemoryStream ms = new MemoryStream();
            Serialize( ms, selfIssued ); 
            row.SetDataField( ms.ToArray() ); 

            // 
            // Save the row to the database
            //
            con.Save( row );
 
            //
            // Update the row id in the object in case 
            // this was an insert. 
            //
            m_rowId = row.LocalId; 
        }

        //
        // Summary 
        // Encrypts a collection of claim values in preparation for saving.
        // 
        // Parameters 
        // pinHelper - A helper used to hold the key and related info.
        // 
        public void Encrypt( PinProtectionHelper pinHelper )
        {
            foreach( KeyValuePair pair in this )
            { 
                if( pair.Value.Value.Length > 0 )
                { 
                    pair.Value.Encrypt( pinHelper ); 
                }
            } 
        }

        //
        // Summary 
        // Decrypts a collection of claim values using the specified key information.
        // 
        // Parameters 
        // pinHelper - A helper used to hold the key and related info.
        // 
        public void Decrypt( PinProtectionHelper pinHelper )
        {

            foreach( KeyValuePair pair in this ) 
            {
                if( pair.Value.Value.Length > 0 ) 
                { 
                    pair.Value.Decrypt( pinHelper );
 
                    //
                    // Check values for gender and date of birth
                    //
                    if( !String.IsNullOrEmpty( pair.Value.Value ) ) 
                    {
                        if( InfoCardConstants.Gender == pair.Key ) 
                        { 
                            bool valid = ( "0" == pair.Value.Value ||
                              "1" == pair.Value.Value || 
                              "2" == pair.Value.Value );

                            if( !valid )
                            { 
                                throw IDT.ThrowHelperError(
                                    new InvalidCardException( SR.GetString( SR.ServiceInvalidEncryptedClaimValues ) ) ); 
                            } 
                        }
                        if( ( InfoCardConstants.DateOfBirth == pair.Key ) ) 
                        {
                            try
                            {
                                DateTime dob = XmlConvert.ToDateTime( pair.Value.Value, XmlDateTimeSerializationMode.Utc ); 
                            }
                            catch( Exception e ) 
                            { 
                                if( IDT.IsFatal( e ) )
                                { 
                                    throw;
                                }

                                throw IDT.ThrowHelperError( 
                                    new InvalidCardException( SR.GetString( SR.ServiceInvalidEncryptedClaimValues ) ) );
                            } 
                        } 
                    }
                } 
            }
        }

        // 
        // Summary
        // Makes a deep copy of the claim 
        // 
        // Returns the copy
        // 
        public InfoCardClaimCollection Clone()
        {
            InfoCardClaimCollection claims = new InfoCardClaimCollection( new Uri( this.m_infoCardId.OriginalString ) );
            claims.m_rowId = this.m_rowId; 

            foreach( KeyValuePair pair in this ) 
            { 
                claims[ (string) pair.Key.Clone() ] = pair.Value.Clone();
            } 

            return claims;
        }
 
        //
        // Summary 
        // Queries the store for the claims object associated with the InfoCard Id 
        // currently in m_infoCardId.
        // 
        // Remarks
        // This function will return null if no object is found in the store.
        //
        // Parameters 
        // con      - Connection to the store to be used to find the claims collection object.
        // details  - The projection of the store information to be returned. 
        // 
        protected DataRow TryGetRow( StoreConnection con, QueryDetails details )
        { 
            IDT.Assert( null != m_infoCardId, "null infocard id" );

            //
            // Retrieve a single object from the database 
            //
            DataRow row = 
                con.GetSingleRow( details, 
                           new QueryParameter( SecondaryIndexDefinition.ObjectTypeIndex,
                           ( Int32 )StorableObjectType.InfoCardClaims ), 
                           new QueryParameter( SecondaryIndexDefinition.ParentIdIndex,
                           GlobalId.DeriveFrom( m_infoCardId.ToString() ) ) );

            return row; 
        }
    } 
 
}
 



// 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