예제 #1
0
        private string SerializeResponse(RequestSecurityTokenResponse response)
        {
            var serializer = new WSTrust13ResponseSerializer();
            var context =
                new WSTrustSerializationContext(
                    FederatedAuthentication.FederationConfiguration.IdentityConfiguration
                        .SecurityTokenHandlerCollectionManager);
            var sb = new StringBuilder(128);

            using (var writer = XmlWriter.Create(new StringWriter(sb)))
            {
                serializer.WriteXml(response, writer, context);
                return sb.ToString();
            }
        }
        public string Serialize(RequestSecurityTokenResponse securityTokenResponse)
        {
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.OmitXmlDeclaration = true;

            using (MemoryStream memoryStream = new MemoryStream())
            using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings))
            {
                WSTrust13ResponseSerializer serializer = new WSTrust13ResponseSerializer();
                WSTrustSerializationContext context = new WSTrustSerializationContext();
                serializer.WriteXml(securityTokenResponse, xmlWriter, context);
                xmlWriter.Flush();
                var encoding = new System.Text.UTF8Encoding(false);
                return encoding.GetString(memoryStream.ToArray());

            }
        }
        /// <summary>
        /// Reads the 'wresult' and returns the embeded security token.
        /// </summary>
        /// <returns>the 'SecurityToken'.</returns>
        public virtual string GetToken()
        {
            if (Wresult == null)
            {
                return null;
            }

            using (StringReader sr = new StringReader(Wresult))
            {
                XmlReader xmlReader = XmlReader.Create(sr);
                xmlReader.MoveToContent();

                WSTrustResponseSerializer serializer = new WSTrust13ResponseSerializer();
                if (serializer.CanRead(xmlReader))
                {
                    RequestSecurityTokenResponse response = serializer.ReadXml(xmlReader, new WSTrustSerializationContext());
                    return response.RequestedSecurityToken.SecurityTokenXml.OuterXml;
                }

                serializer = new WSTrustFeb2005ResponseSerializer();
                if (serializer.CanRead(xmlReader))
                {
                    RequestSecurityTokenResponse response = serializer.ReadXml(xmlReader, new WSTrustSerializationContext());
                    return response.RequestedSecurityToken.SecurityTokenXml.OuterXml;
                }
            }

            return null;
        }