internal override void WriteTo(XmlWriter writer)
            {
                writer.WriteStartElement(ElementName);
                writer.WriteAttributeString(DiagnosticStrings.NamespaceTag, EventId);

                writer.WriteStartElement("SecurityToken");
                writer.WriteAttributeString("Type", _securityToken.GetType().ToString());

                if (_securityToken is SessionSecurityToken)
                {
                    WriteSessionToken(writer, _securityToken as SessionSecurityToken);
                }
                else
                {
                    SecurityTokenHandlerCollection sthc = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();
                    if (sthc.CanWriteToken(_securityToken))
                    {
                        {
                            sthc.WriteToken(writer, _securityToken);
                        }
                    }
                    else
                    {
                        writer.WriteElementString("Warning", SR.GetString(SR.TraceUnableToWriteToken, _securityToken.GetType().ToString()));
                    }
                }

                writer.WriteEndElement();
            }
        /// <summary>Converts a supported token to an XML string.</summary>
        /// <param name="token">The token.</param>
        /// <param name="handler">The token handler.</param>
        /// <returns>The token XML string.</returns>
        public static string ToTokenXmlString(this SecurityToken token, SecurityTokenHandlerCollection handler)
        {
            if (!handler.CanWriteToken(token))
            {
                throw new InvalidOperationException("Token type not supported");
            }
            var sb = new StringBuilder(128);

            handler.WriteToken(new XmlTextWriter(new StringWriter(sb)), token);
            return(sb.ToString());
        }
        /// <summary>
        /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer
        /// can write the given security token.
        /// </summary>
        /// <param name="token">SecurityToken instance.</param>
        /// <returns>'True' if the serializer can write the given security token.</returns>
        /// <exception cref="ArgumentNullException">The input parameter 'token' is null.</exception>
        protected override bool CanWriteTokenCore(SecurityToken token)
        {
            if (token == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token");
            }

            if (_securityTokenHandlers.CanWriteToken(token))
            {
                return(true);
            }

            return(base.CanWriteTokenCore(token));
        }
        private static string SerializeToken(SimpleWebToken accessToken, SecurityTokenHandlerCollection handlers)
        {
            if (handlers.CanWriteToken(accessToken))
            {
                string token = String.Empty;
                using (var sw = new StringWriter())
                {
                    var writer = new XmlTextWriter(sw);
                    handlers.WriteToken(writer, accessToken);

                    // remove the envelope <stringToken>
                    var envelope = sw.ToString();
                    token = XElement.Parse(envelope).Value;
                }

                return token;
            }

            return null;
        }
Exemple #5
0
        private static string SerializeToken(SimpleWebToken accessToken, SecurityTokenHandlerCollection handlers)
        {
            if (handlers.CanWriteToken(accessToken))
            {
                string token = String.Empty;
                using (var sw = new StringWriter())
                {
                    var writer = new XmlTextWriter(sw);
                    handlers.WriteToken(writer, accessToken);

                    // remove the envelope <stringToken>
                    var envelope = sw.ToString();
                    token = XElement.Parse(envelope).Value;
                }

                return(token);
            }

            return(null);
        }
        void WriteToken(XmlWriter xmlWriter, SecurityToken token, string usage)
        {
            SecurityTokenHandlerCollection tokenHandlerCollection = null;

            if (this.tokenHandlerCollectionManager.ContainsKey(usage))
            {
                tokenHandlerCollection = this.tokenHandlerCollectionManager[usage];
            }
            else
            {
                tokenHandlerCollection = this.tokenHandlerCollectionManager[SecurityTokenHandlerCollectionManager.Usage.Default];
            }

            if (tokenHandlerCollection != null && tokenHandlerCollection.CanWriteToken(token))
            {
                tokenHandlerCollection.WriteToken(xmlWriter, token);
            }
            else
            {
                SecurityTokenSerializer.WriteToken(xmlWriter, token);
            }
        }
 /// <summary>
 /// Converts a supported token to an XML string.
 /// </summary>
 /// <param name="token">The token.</param>
 /// <param name="handler">The token handler.</param>
 /// <returns>The token XML string.</returns>
 public static string ToTokenXmlString(this SecurityToken token, SecurityTokenHandlerCollection handler)
 {
     if (handler.CanWriteToken(token))
     {
         var sb = new StringBuilder(128);
         handler.WriteToken(new XmlTextWriter(new StringWriter(sb)), token);
         return sb.ToString();
     }
     else
     {
         throw new InvalidOperationException("Token type not suppoted");
     }
 }