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

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.InfoCards
{ 
    using System;
    using System.Collections.Generic; 
    using System.IO; 
    using System.ServiceModel;
    using System.ServiceModel.Channels; 
    using System.Text;
    using System.Xml;
    using System.Security.Cryptography.X509Certificates;
    using System.Xml.Serialization; 
    using System.Xml.Schema;
    using Microsoft.InfoCards.Diagnostics; 
    using System.Security.Cryptography; 
    using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;
 
    //
    // Summary
    //  An authentication mechanism a card can use to authenticate to the
    //  identity provider. 
    //
    internal class TokenCreationParameter :IXmlSerializable 
    { 
        TokenFactoryCredentialType m_credentialType;
        EndpointAddress m_epr;                 // EPR 
        string m_hint;
        Dictionary m_selectorMap;

        public TokenCreationParameter() 
        {
            m_selectorMap = new Dictionary(); 
        } 

        // 
        // Summary:
        //  Gets the type of credential created by this parameter.
        //
        public TokenFactoryCredentialType CredentialType 
        {
            get { return m_credentialType; } 
            set { m_credentialType = value; } 
        }
        public EndpointAddress Epr 
        {
            get { return m_epr; }
            set { m_epr = value; }
        } 
        public Dictionary CredentialSelectors
        { 
            get { return m_selectorMap; } 
        }
 

        public bool IsComplete()
        {
 
            if( null == m_epr )
            { 
                return false; 
            }
 
            bool complete = true;

            //
            // For x509certificate credential either a keyidentifier or a issuer name + issuer serial no is required 
            // and for self issued a PPID is required
            // 
            if( m_credentialType == TokenFactoryCredentialType.X509CertificateCredential 
                || m_credentialType == TokenFactoryCredentialType.SelfIssuedCredential )
            { 
                complete = complete && m_selectorMap.Count > 0;
            }
            return complete;
        } 

 
        public void ThrowIfNotComplete() 
        {
            if( !IsComplete() ) 
            {
                throw IDT.ThrowHelperError( new SerializationIncompleteException(
                    this.GetType() ) );
            } 
        }
 
 
        //
        // Summary 
        //  Serialize the TokenCreationParameter object
        //
        // Parameter
        //   writer - binary stream conforming to the serialization format supported by this class. 
        //
        public void Serialize( BinaryWriter writer ) 
        { 
            ThrowIfNotComplete();
 
            writer.Write( (int)m_credentialType );

            Utility.SerializeString( writer, m_hint );
 
            Utility.SerializeString(
                        writer, 
                        CreateEprXmlString( m_epr ) ); 

            writer.Write( m_selectorMap.Count ); 
            if( m_selectorMap.Count > 0 )
            {
                foreach( CredentialSelector selector in m_selectorMap.Values )
                { 
                    selector.Serialize( writer );
                } 
            } 

 
        }

        //
        // Summary 
        //  Deserialize the TokenCreationParameter object
        // 
        // Parameter 
        //   reader - binary stream conforming to the serialization format supported by this class.
        // 
        public void Deserialize( BinaryReader reader )
        {
            TokenFactoryCredentialType type;
            EndpointAddress endpoint; 
            int selectorCount;
            Dictionary selectors; 
 
            type = (TokenFactoryCredentialType)reader.ReadInt32();
            string hint = Utility.DeserializeString( reader ); 

            string epr = Utility.DeserializeString( reader );

            endpoint = CreateEprFromString( epr ); 

 
            selectorCount = reader.ReadInt32(); 
            selectors = new Dictionary();
            for( int i = 0; i < selectorCount; i++ ) 
            {
                CredentialSelector selector = new CredentialSelector();
                selector.Deserialize( reader );
                selectors.Add( selector.Type, selector ); 
            }
 
            m_credentialType = type; 
            m_epr = endpoint;
            m_selectorMap = selectors; 
            m_hint = hint;

            ThrowIfNotComplete();
 
        }
 
        // 
        // Summary:
        //  Creates a new EndpointAddress from the unicode string. 
        //
        static EndpointAddress CreateEprFromString( string value )
        {
            using( MemoryStream stream = new MemoryStream( Encoding.UTF8.GetBytes( value ) ) ) 
            {
                using( XmlDictionaryReader reader = Utility.CreateReaderWithQuotas( stream ) ) 
                { 
                    try
                    { 
                        return EndpointAddress.ReadFrom( reader );
                    }
                    catch( InfoCardBaseException )
                    { 
                        throw;
                    } 
                    catch( Exception e ) 
                    {
                        if( IDT.IsFatal( e ) ) 
                        {
                            throw;
                        }
                        throw IDT.ThrowHelperError( 
                                        new InvalidCardException(
                                                    SR.GetString( 
                                                            SR.ServiceInvalidCredentialSelector ), e ) ); 
                    }
                } 
            }
        }

        // 
        // Summary:
        //  Creates a unicode string of xml from and EndpointAddress 
        // 
        static string CreateEprXmlString( EndpointAddress address )
        { 
            using( MemoryStream buffer = new MemoryStream() )
            {
                using( XmlWriter xmlw = new XmlTextWriter( buffer, System.Text.Encoding.Unicode ) )
                { 
                    address.WriteTo( AddressingVersion.WSAddressing10, xmlw );
                    xmlw.Flush(); 
                    buffer.Flush(); 

                    // 
                    // Create the string, but skip the BOM
                    //
                    return Encoding.Unicode.GetString(
                                    buffer.GetBuffer(), 
                                    2,
                                    Convert.ToInt32( buffer.Length - 2 ) ); 
                } 
            }
 

        }

        public XmlSchema GetSchema() 
        {
            return null; 
        } 

 
        //
        // Summary
        //  Write the TokenCreationParameter to xml.
        // 
        // Parameters
        //   writer - The XmlWriter to write the data to 
        // 
        public void WriteXml( XmlWriter writer )
        { 
            if( null == writer )
            {
                throw IDT.ThrowHelperArgumentNull( "writer" );
            } 

            IDT.ThrowInvalidArgumentConditional( !IsComplete(), "IsComplete" ); 
 
            writer.WriteStartElement( XmlNames.WSIdentity.TokenServiceElement, XmlNames.WSIdentity.Namespace );
 
            //
            // Write the EndPointReference
            //
            m_epr.WriteTo( AddressingVersion.WSAddressing10, writer ); 

            // 
            // Write the UserCredential Element 
            //
            writer.WriteStartElement( XmlNames.WSIdentity.UserCredentialElement, XmlNames.WSIdentity.Namespace ); 

            //
            // Write the hint
            // 
            if( !String.IsNullOrEmpty( m_hint ) )
            { 
                writer.WriteStartElement( XmlNames.WSIdentity.DisplayCredentialHintElement, XmlNames.WSIdentity.Namespace ); 
                writer.WriteString( m_hint );
                writer.WriteEndElement(); 
            }

            //
            // write the authentication credential information. 
            //
            switch( m_credentialType ) 
            { 
                case TokenFactoryCredentialType.UserNamePasswordCredential:
                    { 
                        WriteUserNameCredential( writer );
                    }
                    break;
                case TokenFactoryCredentialType.KerberosCredential: 
                    {
                        WriteKerberosCredential( writer ); 
                    } 
                    break;
                case TokenFactoryCredentialType.SelfIssuedCredential: 
                    {
                        WriteSelfIssuedCredential( writer );
                    }
                    break; 
                case TokenFactoryCredentialType.X509CertificateCredential:
                    { 
                        WriteX509CertificateCredential( writer ); 
                    }
                    break; 
                default:
                    IDT.ThrowInvalidArgumentConditional( true, "CredentialType" );
                    break;
            } 
            writer.WriteEndElement(); // End of UserCredential
 
            writer.WriteEndElement(); // End of TokenService 
        }
 
        //
        // Summary:
        //  Write the UserName credential information.
        // 
        // Arguments:
        //  writer:     The XmlWriter to write the xml information to. 
        // 
        public void WriteUserNameCredential( XmlWriter writer )
        { 
            writer.WriteStartElement( XmlNames.WSIdentity.UserNamePasswordCredentialElement, XmlNames.WSIdentity.Namespace );
            if( m_selectorMap.ContainsKey( CredentialSelectorType.UserNameSelector ) )
            {
                m_selectorMap[ CredentialSelectorType.UserNameSelector ].WriteXml( writer ); 
            }
            writer.WriteEndElement(); 
        } 

        // 
        // Summary:
        //  Write the kerberos information.
        //
        // Arguments: 
        //  writer:     The XmlWriter to write the xml information to.
        // 
        public void WriteKerberosCredential( XmlWriter writer ) 
        {
            writer.WriteStartElement( XmlNames.WSIdentity.KerberosV5CredentialElement, XmlNames.WSIdentity.Namespace ); 
            writer.WriteEndElement();
        }

        // 
        // Summary:
        //  Write the SelfIssued credential information. 
        // 
        // Arguments:
        //  writer:     The XmlWriter to write the xml information to. 
        //
        public void WriteSelfIssuedCredential( XmlWriter writer )
        {
            writer.WriteStartElement( XmlNames.WSIdentity.SelfIssuedCredentialElement, XmlNames.WSIdentity.Namespace ); 
            if( m_selectorMap.ContainsKey( CredentialSelectorType.SelfIssuedCardIdSelector ) )
            { 
                m_selectorMap[ CredentialSelectorType.SelfIssuedCardIdSelector ].WriteXml( writer ); 
            }
            writer.WriteEndElement(); 
        }

        //
        // Summary: 
        //  Write the x509 certificate credential information.
        // 
        // Arguments: 
        //  writer:     The XmlWriter to write the xml information to.
        // 
        public void WriteX509CertificateCredential( XmlWriter writer )
        {
            writer.WriteStartElement( XmlNames.WSIdentity.X509V3CredentialElement, XmlNames.WSIdentity.Namespace );
            if( m_selectorMap.ContainsKey( CredentialSelectorType.X509CertificateIssuerNameSelector ) 
                && m_selectorMap.ContainsKey( CredentialSelectorType.X509CertificateIssuerSerialNoSelector ) )
            { 
                writer.WriteStartElement( XmlNames.XmlDSig.X509DataElement, XmlNames.XmlDSig.Namespace ); 
                writer.WriteStartElement( XmlNames.XmlDSig.X509IssuerSerialElement, XmlNames.XmlDSig.Namespace );
                m_selectorMap[ CredentialSelectorType.X509CertificateIssuerNameSelector ].WriteXml( writer ); 
                m_selectorMap[ CredentialSelectorType.X509CertificateIssuerSerialNoSelector ].WriteXml( writer );
                writer.WriteEndElement();
                writer.WriteEndElement();
            } 
            else if( m_selectorMap.ContainsKey( CredentialSelectorType.X509CertificateKeyHashSelector ) )
            { 
                writer.WriteStartElement( XmlNames.XmlDSig.X509DataElement, XmlNames.XmlDSig.Namespace ); 
                m_selectorMap[ CredentialSelectorType.X509CertificateKeyHashSelector ].WriteXml( writer );
                writer.WriteEndElement(); 
            }
            writer.WriteEndElement();
        }
 

 
        // 
        // Summary
        //  Read the TokenCreationParameter from the xml. The reader should be the 
        //  TokenServiceElement element.
        //
        // Parameters
        //   reader - The XmlReader to read data from 
        //
        public void ReadXml( XmlReader reader ) 
        { 
            if( !reader.IsStartElement( XmlNames.WSIdentity.TokenServiceElement, XmlNames.WSIdentity.Namespace ) )
            { 
                throw IDT.ThrowHelperError( new XmlException( SR.GetString( SR.UnexpectedElement ) ) );
            }

            reader.ReadStartElement(); 
            //
            // Read the EPR 
            // 
            if( reader.LocalName == XmlNames.WSAddressing.EndpointReference )
            { 
                try
                {
                    m_epr = EndpointAddress.ReadFrom( XmlDictionaryReader.CreateDictionaryReader( reader ) );
                } 
                catch( InfoCardBaseException )
                { 
                    throw; 
                }
                catch( Exception e ) 
                {
                    if( IDT.IsFatal( e ) )
                    {
                        throw; 
                    }
                    throw IDT.ThrowHelperError( 
                                    new InvalidCardException( 
                                                SR.GetString(
                                                        SR.ServiceInvalidCredentialSelector ), e ) ); 
                }

                //
                // make sure that the card has a valid nmex endpoint 
                //
                if( null != m_epr && null == Utility.DeriveMexAddress( m_epr ) ) 
                { 
                    throw IDT.ThrowHelperError( new InvalidCardException( SR.GetString( SR.ServiceEprDoesNotHaveValidMetadata ) ) );
                } 
            }
            else
            {
                throw IDT.ThrowHelperError( new InvalidCardException( SR.GetString( SR.ServiceInvalidCredentialSelector ) ) ); 

            } 
 
            if( reader.LocalName == XmlNames.WSIdentity.UserCredentialElement )
            { 
                reader.ReadStartElement();
            }
            //
            // Read the credential hint 
            //
            if( reader.LocalName == XmlNames.WSIdentity.DisplayCredentialHintElement ) 
            { 
                m_hint = ReadDisplayCredentialHint( reader );
            } 

            //
            // Read the selector information
            // 
            switch( reader.LocalName )
            { 
                // 
                // Read usernamepassword authentication details
                // 
                case XmlNames.WSIdentity.UserNamePasswordCredentialElement:
                    {
                        this.CredentialType = TokenFactoryCredentialType.UserNamePasswordCredential;
                        if( !reader.IsEmptyElement ) 
                        {
                            while( reader.Read() ) 
                            { 
                                if( reader.LocalName == XmlNames.WSIdentity.UserNamePasswordCredentialElement
                                    && reader.NodeType == XmlNodeType.EndElement ) 
                                {
                                    break;
                                }
                                if( reader.LocalName == XmlNames.WSIdentity.UserNameElement ) 
                                {
                                    CredentialSelector selector = new CredentialSelector(); 
                                    selector.ReadXml( reader ); 
                                    if( selector.IsComplete() )
                                    { 
                                        this.CredentialSelectors.Add( selector.Type, selector );
                                    }
                                    break;
                                } 
                            }
                        } 
                        else 
                        {
                            reader.ReadStartElement(); 
                        }

                    }
                    break; 
                //
                // Read kerberos authentication details 
                // 
                case XmlNames.WSIdentity.KerberosV5CredentialElement:
                    { 
                        this.CredentialType = TokenFactoryCredentialType.KerberosCredential;
                        if( !reader.IsEmptyElement )
                        {
                            while( reader.Read() ) 
                            {
                                if( reader.LocalName == XmlNames.WSIdentity.KerberosV5CredentialElement 
                                    && reader.NodeType == XmlNodeType.EndElement ) 
                                {
                                    break; 
                                }
                                if( reader.LocalName == XmlNames.WSIdentity.UserPrincipalNameElement )
                                {
                                    CredentialSelector selector = new CredentialSelector(); 
                                    selector.ReadXml( reader );
                                    if( selector.IsComplete() ) 
                                    { 
                                        this.CredentialSelectors.Add( selector.Type, selector );
                                    } 
                                    break;
                                }
                            }
                        } 
                        else
                        { 
                            reader.ReadStartElement(); 
                        }
 
                    }
                    break;
                //
                // Read self issued token authentication details 
                //
                case XmlNames.WSIdentity.SelfIssuedCredentialElement: 
                    { 
                        this.CredentialType = TokenFactoryCredentialType.SelfIssuedCredential;
                        reader.ReadStartElement(); 
                        CredentialSelector selector = new CredentialSelector();
                        selector.ReadXml( reader );
                        if( selector.IsComplete() )
                        { 
                            this.CredentialSelectors.Add( selector.Type, selector );
                        } 
                        reader.ReadEndElement(); 

                    } 
                    break;
                //
                // Read certificate authentication details
                // 
                case XmlNames.WSIdentity.X509V3CredentialElement:
                    { 
                        this.CredentialType = TokenFactoryCredentialType.X509CertificateCredential; 
                        ReadX509V3CredentialElement( reader );
                        reader.ReadEndElement(); 
                    }
                    break;
                default:
                    throw IDT.ThrowHelperError( 
                        new InvalidCardException( SR.GetString( SR.NoAuthenticationServicesInCard ) ) );
 
            } 
            reader.ReadEndElement(); //read the UserCredential element end
 
        }


 
        //
        // Summary: 
        //  Read the credential hint 
        //
        // Parameters: 
        //  reader:     The XmlReader to read the xml information from.
        //
        // Return
        //  The hint string. 
        public string ReadDisplayCredentialHint( XmlReader reader )
        { 
            if( !reader.IsStartElement( XmlNames.WSIdentity.DisplayCredentialHintElement, XmlNames.WSIdentity.Namespace ) ) 
            {
                throw IDT.ThrowHelperError( new XmlException( SR.GetString( SR.UnexpectedElement ) ) ); 
            }
            IDT.TraceDebug( "Reading the credential hint information" );

            return reader.ReadElementContentAsString().Trim(); 

        } 
 

        // 
        // Summary:
        //  Read the X509Software and SmartCard credential selector information.
        //
        // Arguments: 
        //  reader:     The XmlReader to read the xml information from.
        // 
        void ReadX509V3CredentialElement( XmlReader reader ) 
        {
            if( !reader.IsStartElement( XmlNames.WSIdentity.X509V3CredentialElement, XmlNames.WSIdentity.Namespace ) ) 
            {
                throw IDT.ThrowHelperError( new XmlException( SR.GetString( SR.UnexpectedElement ) ) );
            }
            IDT.TraceDebug( "Reading the X509 certificate authentication information" ); 

            reader.ReadStartElement(); 
 
            //
            // Read the X509 data element 
            //
            if( XmlNames.XmlDSig.Namespace == reader.NamespaceURI &&
                        XmlNames.XmlDSig.X509DataElement == reader.LocalName )
            { 

                while( reader.Read() ) 
                { 
                    //
                    // Read the X509IssuerSerial element 
                    //
                    if( reader.IsStartElement(
                                    XmlNames.XmlDSig.X509IssuerSerialElement,
                                    XmlNames.XmlDSig.Namespace ) ) 
                    {
                        reader.ReadStartElement(); 
 
                        //
                        // read the X509SerialNumber element 
                        //
                        CredentialSelector X509SerialNumberSelector = new CredentialSelector();
                        X509SerialNumberSelector.ReadXml( reader );
                        // 
                        // read the X509IssuerName element
                        // 
                        CredentialSelector X509IssuerNameSelector = new CredentialSelector(); 
                        X509IssuerNameSelector.ReadXml( reader );
 
                        //
                        // Both credentials should have been read correctly
                        //
                        if( X509IssuerNameSelector.IsComplete() && X509SerialNumberSelector.IsComplete() ) 
                        {
                            this.CredentialSelectors.Add( X509SerialNumberSelector.Type, X509SerialNumberSelector ); 
                            this.CredentialSelectors.Add( X509IssuerNameSelector.Type, X509IssuerNameSelector ); 
                        }
 
                        reader.ReadEndElement();
                        break;
                    }
 
                    //
                    // Read the KeyIdentifier element 
                    // 
                    if( reader.IsStartElement(
                                    XmlNames.WSSecurityExt.KeyIdentifierElement, 
                                    XmlNames.WSSecurityExt.Namespace ) )
                    {
                        CredentialSelector selector = new CredentialSelector();
                        selector.ReadXml( reader ); 
                        if( selector.IsComplete() )
                        { 
                            this.CredentialSelectors.Add( selector.Type, selector ); 
                        }
                        break; 

                    }
                }
 
                //
                // return if reached the end of the X509 data element 
                // 
                do
                { 
                    if( XmlNames.XmlDSig.X509DataElement == reader.LocalName
                        && XmlNames.XmlDSig.Namespace == reader.NamespaceURI
                        && XmlNodeType.EndElement == reader.NodeType )
                    { 
                        reader.ReadEndElement();
                        return; 
                    } 

                } while( reader.Read() ); 

            }

 

        } 
 
    }
 

}

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