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

                            namespace Microsoft.InfoCards 
{
    using System;
    using System.Collections;
    using System.Collections.Generic; 
    using System.Collections.ObjectModel;
    using System.Diagnostics; 
    using System.IO; 
    using System.ServiceModel.Security;
    using System.ServiceModel; 
    using System.ServiceModel.Channels;
    using System.Security.Cryptography;
    using System.Security.Cryptography.Xml;
    using System.Runtime.Serialization; 
    using System.Globalization;
    using System.Xml; 
    using System.IdentityModel.Tokens; 
    using System.ServiceModel.Security.Tokens;
 
    using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;
    using System.Text;

 
    internal class RequestSecurityTokenSerializer
    { 
        private XmlWriter m_writer; 
        private ProtocolProfile m_profile;
 
        public XmlWriter Writer
        {
            get
            { 
                return m_writer;
            } 
        } 
        public ProtocolProfile Profile
        { 
            get
            {
                return m_profile;
            } 
        }
 
        private XmlNames.IWSTrust WSTrust 
        {
            get 
            {
                return Profile.WSTrust;
            }
        } 

        private XmlNames.IWSPolicy WSPolicy 
        { 
            get
            { 
                return Profile.WSPolicy;
            }
        }
 
        private string WSTPrefix
        { 
            get 
            {
                return Profile.WSTrust.DefaultPrefix; 
            }
        }

        private string WSIPrefix 
        {
            get 
            { 
                return XmlNames.WSIdentity.DefaultPrefix;
            } 
        }

        private string WSPPrefix
        { 
            get
            { 
                return Profile.WSPolicy.DefaultPrefix; 
            }
        } 

        private string DSIGPrefix
        {
            get 
            {
                return XmlNames.XmlDSig.DefaultPrefix; 
            } 
        }
 
        public RequestSecurityTokenSerializer( XmlWriter writer, ProtocolProfile profile )
        {
            m_writer = writer;
            m_profile = profile; 
        }
 
        public void WriteClaimElement( string claimUri, bool bIsOptionalClaim ) 
        {
            Writer.WriteStartElement( WSIPrefix, XmlNames.WSIdentity.ClaimElement, XmlNames.WSIdentity.Namespace ); 
            Writer.WriteAttributeString( XmlNames.WSIdentity.UriAttribute, claimUri );
            if( bIsOptionalClaim )
            {
                Writer.WriteAttributeString( XmlNames.WSIdentity.OptionalAttribute, "true" ); 
            }
            Writer.WriteEndElement(); 
        } 

        // 
        // Writes the Claims element
        //
        // Parameters
        // disclosedClaims- The set of claims that the user has decided to release.  If this value is populated, required and optionalClaims 
        //                  will be ignored.
        // requiredClaims-  The set of claims requird by the RP. 
        // optionalClaims-  The set of claims designated as optional by the RP. 
        //
        public void WriteClaimsElement( Dictionary disclosedClaims, string[] requiredClaims, string[] optionalClaims ) 
        {
            Writer.WriteStartElement( WSTPrefix, WSTrust.ClaimsElement, WSTrust.Namespace );

            // 
            // WS-Trust 1.0 specifies Dialect as optional.  WS-Trust 1.3 specifies it is required, so
            // we emit the Dialect in all cases. 
            // 
            Writer.WriteAttributeString( WSTrust.Dialect, XmlNames.WSIdentity.Namespace );
 
            if( null != disclosedClaims )
            {
                foreach( KeyValuePair claimAndIsOptional in disclosedClaims )
                { 
                    IDT.TraceDebug( "IPSTSCLIENT: Writing claim {0} to RST", claimAndIsOptional.Key );
                    WriteClaimElement( claimAndIsOptional.Key, claimAndIsOptional.Value ); 
                } 
            }
            else 
            {
                foreach( string required in requiredClaims )
                {
                    WriteClaimElement( required, false ); 
                }
                foreach( string optional in optionalClaims ) 
                { 
                    WriteClaimElement( optional, true );
                } 
            }

            Writer.WriteEndElement();
        } 

        public void WriteRequestTypeElement( string requestType ) 
        { 
            Writer.WriteStartElement( WSTPrefix, WSTrust.RequestType, WSTrust.Namespace );
            Writer.WriteString( requestType ); 
            Writer.WriteEndElement();
        }

        public void WritePPIDElement( byte[] ppid ) 
        {
            Writer.WriteStartElement( XmlNames.WSIdentity.ClientPseudonym, XmlNames.WSIdentity.Namespace ); 
            Writer.WriteStartElement( XmlNames.WSIdentity.PPID, XmlNames.WSIdentity.Namespace ); 
            Writer.WriteBase64( ppid, 0, ppid.Length );
            Writer.WriteEndElement(); 
            Writer.WriteEndElement();
        }

        public void WriteAppliesToElement( EndpointAddress address, AddressingVersion version ) 
        {
            Writer.WriteStartElement( WSPPrefix, WSPolicy.AppliesTo, WSPolicy.Namespace ); 
            address.WriteTo( version, Writer ); 
            Writer.WriteEndElement();
        } 

        public void WriteKeySizeElement( string keySize )
        {
            Writer.WriteStartElement( WSTPrefix, WSTrust.KeySize, WSTrust.Namespace ); 
            Writer.WriteString( keySize );
            Writer.WriteEndElement(); 
        } 

        public void WriteKeyTypeElement( string keyType ) 
        {
            Writer.WriteStartElement( WSTPrefix, WSTrust.KeyType, WSTrust.Namespace );
            Writer.WriteString( keyType );
            Writer.WriteEndElement(); 
        }
 
        public void WriteUseKeyElement( XmlElement useKey ) 
        {
            Writer.WriteStartElement( WSTPrefix, WSTrust.UseKey, WSTrust.Namespace ); 
            Writer.WriteStartElement( DSIGPrefix, XmlNames.XmlDSig.KeyInfo, XmlNames.XmlDSig.Namespace );
            Writer.WriteStartElement( DSIGPrefix, XmlNames.XmlDSig.KeyValue, XmlNames.XmlDSig.Namespace );
            useKey.WriteContentTo( Writer );
            Writer.WriteEndElement(); 
            Writer.WriteEndElement();
            Writer.WriteEndElement(); 
        } 

        public void WriteBinarySecretElement( BinarySecretSecurityToken bst ) 
        {
            Writer.WriteStartElement( WSTPrefix, WSTrust.Entropy, WSTrust.Namespace );
            Profile.TokenSerializer.WriteToken( Writer, bst );
            Writer.WriteEndElement(); 
        }
 
        public void WriteInfoCardReferenceElement( InfoCard card ) 
        {
            Writer.WriteStartElement( WSIPrefix, XmlNames.WSIdentity.InfoCardRefElement, XmlNames.WSIdentity.Namespace ); 

            Writer.WriteStartElement( WSIPrefix, XmlNames.WSIdentity.CardIdElement, XmlNames.WSIdentity.Namespace );
            Writer.WriteString( card.Id.ToString() );
            Writer.WriteEndElement(); 

            Writer.WriteStartElement( WSIPrefix, XmlNames.WSIdentity.CardVersionElement, XmlNames.WSIdentity.Namespace ); 
            Writer.WriteString( card.Epoch.ToString( CultureInfo.InvariantCulture ) ); 
            Writer.WriteEndElement();
 
            Writer.WriteEndElement();
        }

        public void WriteDisplayTokenElement( CultureInfo displayCulture ) 
        {
            Writer.WriteStartElement( WSIPrefix, XmlNames.WSIdentity.RequestDisplayTokenElement, XmlNames.WSIdentity.Namespace ); 
            Writer.WriteAttributeString( XmlNames.Xml.DefaultPrefix, XmlNames.Xml.Language, null, displayCulture.TwoLetterISOLanguageName ); 
            Writer.WriteEndElement();
        } 

        public void WriteTokenTypeElement( string tokenType )
        {
            Writer.WriteStartElement( WSTPrefix, WSTrust.TokenType, WSTrust.Namespace ); 
            Writer.WriteString( tokenType );
            Writer.WriteEndElement(); 
        } 

        public void WriteEncryptionAlgorithmElement( string encryptionAlgorithm ) 
        {
            Writer.WriteStartElement( WSTPrefix, WSTrust.EncryptionAlgorithm, WSTrust.Namespace );
            Writer.WriteString( encryptionAlgorithm );
            Writer.WriteEndElement(); 
        }
 
        public void WriteSignWithElement( string signWith ) 
        {
            Writer.WriteStartElement( WSTPrefix, WSTrust.SignWith, WSTrust.Namespace ); 
            Writer.WriteString( signWith );
            Writer.WriteEndElement();
        }
 
        public void WriteEncryptWithElement( string encryptWith )
        { 
            Writer.WriteStartElement( WSTPrefix, WSTrust.EncryptWith, WSTrust.Namespace ); 
            Writer.WriteString( encryptWith );
            Writer.WriteEndElement(); 
        }

        public void WriteKeyWrapAlgorithmElement( string keyWrapAlgorithm )
        { 
            IDT.Assert( ( WSTrust.Version == XmlNames.WSSpecificationVersion.WSTrustOasis2007 ), "KeyWrapAlgorithm element is valid in WS-Trust 1.3 only" );
 
            Writer.WriteStartElement( WSTPrefix, WSTrust.KeyWrapAlgorithm, WSTrust.Namespace ); 
            Writer.WriteString( keyWrapAlgorithm );
            Writer.WriteEndElement(); 
        }

        public void WriteUnprocessedPolicyElements( MemoryStream unprocessedElements )
        { 
            unprocessedElements.Seek( 0, 0 );
            XmlReaderSettings settings = new XmlReaderSettings(); 
            settings.ConformanceLevel = ConformanceLevel.Fragment; 
            XmlReader policyElementReader = XmlReader.Create( unprocessedElements, settings );
            while( !policyElementReader.EOF ) 
            {
                Writer.WriteNode( policyElementReader, true );
            }
        } 

        public void WriteSecondaryParametersElement( string policyXml ) 
        { 
            IDT.Assert( ( WSTrust.Version == XmlNames.WSSpecificationVersion.WSTrustOasis2007 ), "SecondaryParameters element is valid in WS-Trust 1.3 only" );
 
            Writer.WriteStartElement( WSTPrefix, WSTrust.SecondaryParameters, WSTrust.Namespace );

            StringReader stringReader = new StringReader( policyXml );
            XmlReaderSettings settings = new XmlReaderSettings(); 
            settings.ConformanceLevel = ConformanceLevel.Fragment;
 
            XmlReader reader = XmlReader.Create( stringReader, settings ); 

            while( !reader.EOF ) 
            {
                Writer.WriteNode( reader, true );
            }
 
            Writer.WriteEndElement();
        } 
    } 
}

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