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

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
//
// Presharp uses the c# pragma mechanism to supress its warnings. 
// These are not recognised by the base compiler so we need to explictly
// disable the following warnings. See http://winweb/cse/Tools/PREsharp/userguide/default.asp 
// for details. 
//
#pragma warning disable 1634, 1691      // unknown message, unknown pragma 



namespace Microsoft.InfoCards 
{
    using System; 
    using System.IO; 
    using System.Text;
    using System.Collections; 
    using System.Collections.Generic;
    using System.IdentityModel.Claims;
    using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;
 
    //
    // Summary 
    // This class carries the claim value and type information. Each InfoCardClaim consists 
    // of a URI which uniquely identifies the type. A namespace specifying the grouping
    // it is from and also a value. 
    //
    // Remarks
    // Currently the InfoCard system supports only this one abstraction of a claim.
    // More complex claims that will require more additional class abstraction may be added at a later 
    // date.
    // 
    internal class InfoCardClaim 
    {
        string      m_id; 
        string      m_claimValue;
        string      m_displayTag;
        string      m_description;
 
        //
        // Summary 
        // Create an instance of the InfoCardClaim class in the uninitialized state 
        //
        // Remarks 
        // This behavior is needed to support the construction of InfoCardClaims
        // from a serialized binary stream.
        //
        public InfoCardClaim() 
        {
        } 
 
        //
        // Summary 
        // Create an instance of the InfoCardClaim class and initialize
        // it using the specified arguments.
        //
        public InfoCardClaim( string id, string value ) 
            :this( id, value, null, null )
        { 
        } 

        // 
        // Summary
        // Create an instance of the InfoCardClaim class and initialize
        // it using the specified arguments.
        // 
        public InfoCardClaim( string id, string value, string description, string displaytag )
        { 
            if( String.IsNullOrEmpty( id ) ) 
            {
                throw IDT.ThrowHelperArgumentNull( "id" ); 
            }
            m_id = id;
            m_claimValue = value;
            m_displayTag = displaytag; 
            m_description = description;
        } 
 
        public string Id
        { 
            get
            {
                return m_id;
            } 
        }
 
        public  string Value 
        {
            get 
            {
                return m_claimValue;
            }
            set { m_claimValue = value; } 
        }
 
        public string DisplayTag 
        {
            get 
            {
                return m_displayTag;
            }
        } 

        public string Description 
        { 
            get
            { 
                return m_description;
            }
        }
 

        // 
        // Summary 
        // Creates a string containing the value of the Claim.
        // 
        public override string ToString()
        {
            return m_claimValue;
        } 

        // 
        // Summary 
        //  Throws if claim entry has not been populated.
        // 
        public void ThrowIfNotComplete()
        {
            bool isComplete = ( !String.IsNullOrEmpty( m_id ) );
 
            if( !isComplete )
            { 
                throw IDT.ThrowHelperError( new SerializationIncompleteException( this.GetType() ) ); 
            }
        } 

        //
        // Summary
        // This method serializes the claim id and value into a stream 
        // via the specified BinaryWriter.
        // 
        // Remarks 
        // This method is typically called by the InfoCardClaimCollection class
        // as part of it's persistence of the claims information into the 
        // store.
        //
        // Parameters
        // writer   - The writer to be used to serialize the id and value for the claim. 
        //
        public void Serialize( BinaryWriter writer ) 
        { 
            ThrowIfNotComplete();
 
            if( null == writer )
            {
                throw IDT.ThrowHelperArgumentNull( "writer" );
            } 

            // 
            // Write each member to binary stream 
            //
            Utility.SerializeString( writer, m_id ); 
            Utility.SerializeString( writer, m_displayTag );
            Utility.SerializeString( writer, m_description );
            Utility.SerializeString( writer, m_claimValue );
        } 

        // 
        // Summary 
        // Populates the instance with the information in the stream wrapper
        // by the BinaryReader provided. 
        //
        // Remarks
        // This method is typically called by the InfoCardClaimCollection class
        // as part of it's construction to create each claim value. 
        //
        // Parameters 
        // reader   - The reader to be used to deserialize the id and value for the claim. 
        //
        public void DeSerialize( BinaryReader reader ) 
        {
            m_id = Utility.DeserializeString( reader );
            m_displayTag = Utility.DeserializeString( reader );
            m_description = Utility.DeserializeString( reader ); 
            m_claimValue = Utility.DeserializeString( reader );
        } 
 
        //
        // Summary 
        // Encrypts a claim value using the specified key information
        //
        // Parameters
        // pinHelper - A helper used to hold the key and related info. 
        //
        public void Encrypt( PinProtectionHelper pinHelper ) 
        { 
            UnicodeEncoding unicode = new UnicodeEncoding();
            byte[] toEncrypt = unicode.GetBytes( m_claimValue ); 
            m_claimValue = Convert.ToBase64String( pinHelper.Encrypt( toEncrypt ) );
        }

        // 
        // Summary
        // Decrypts a claim value using the specified key information. 
        // 
        // Parameters
        // pinHelper - A helper used to hold the key and related info. 
        //
        public void Decrypt( PinProtectionHelper pinHelper )
        {
            UnicodeEncoding unicode = new UnicodeEncoding(); 
            byte[] toDecrypt = Convert.FromBase64String( m_claimValue );
            m_claimValue = unicode.GetString( pinHelper.Decrypt( toDecrypt ) ); 
        } 

        // 
        // Summary
        // Makes a deep copy of the claim
        //
        // Returns the copy 
        //
        public InfoCardClaim Clone() 
        { 
            return new InfoCardClaim(
                                (string) m_id.Clone(), 
                                ( String.IsNullOrEmpty( m_claimValue ) ? null : (string) m_claimValue.Clone() ),
                                ( String.IsNullOrEmpty( m_description ) ? null : (string) m_description.Clone() ),
                                ( String.IsNullOrEmpty( m_displayTag ) ? null : (string) m_displayTag.Clone() ) );
        } 
    }
} 
 

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