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()); }
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; }
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); }
public static string SerializeToken(SecurityToken token, bool urlEncode = false) { StringBuilder sb = new StringBuilder(); if (token is GenericXmlSecurityToken) { WSSecurityTokenSerializer wss = new WSSecurityTokenSerializer(SecurityVersion.WSSecurity11); XmlWriter writer = XmlWriter.Create(sb); wss.WriteToken(writer, token); } else { SecurityTokenHandlerCollection handlers = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers; handlers.WriteToken(new XmlTextWriter(new StringWriter(sb)), token); } string serialized = sb.ToString(); return(urlEncode ? HttpUtility.UrlEncode(serialized) : serialized); }
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"); } }