c# Get SecurityToken from SAML2 Assertion

c# Get SecurityToken from SAML2 Assertion

Following snippet will do the trick:

//read the saml data from request string samlResponse = Request["SMALResponse"];
// get the base64 encoded bytes from the saml data byte[] samlData = Convert.FromBase64String( samlResponse ); // convert it as UTF string string samlAssertion = Encoding.UTF8.GetString(samlData); SecurityToken token; using (StringReader sr = new StringReader(samlAssertion)) { using (XmlReader reader = XmlReader.Create(sr)) { if (!reader.ReadToFollowing("saml:Assertion")) { throw new Exception("Assertion not found!"); } SecurityTokenHandlerCollection collection = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection(); token = collection.ReadToken(reader.ReadSubtree()); System.Diagnostics.Trace.WriteLine( " ID:" + token.Id); } }

Comments (0)

This thread has been closed from taking new comments.