public Uri BuildRedirectUri(AuthnRequestContext authnRequestContext) { var configuration = authnRequestContext.Configuration; var authnRequest = new AuthnRequest { Id = "Imperial.flowz.co.uk", IsPassive = false, Destination = authnRequestContext.Destination.AbsoluteUri, Version = "2.0", IssueInstant = DateTime.UtcNow }; authnRequest.Issuer = new NameId { Value = "Imperial.flowz.co.uk" }; var audienceRestrictions = new List <ConditionAbstract>(); var audienceRestriction = new AudienceRestriction { Audience = new List <string>() { "Imperial.flowz.co.uk" } }; audienceRestrictions.Add(audienceRestriction); authnRequest.Conditions = new Conditions { Items = audienceRestrictions }; var serialiser = new XMLSerialiser(); serialiser.XmlNamespaces.Add("samlp", Saml20Constants.Protocol); serialiser.XmlNamespaces.Add("saml", Saml20Constants.Assertion); var sb = new StringBuilder(); using (var ms = new MemoryStream()) { serialiser.Serialize(ms, new[] { authnRequest }); ms.Position = 0; var streamReader = new StreamReader(ms); var xmlString = streamReader.ReadToEnd(); ms.Position = 0; var encoded = this.DeflateEncode(xmlString); var encodedEscaped = Uri.EscapeDataString(this.UpperCaseUrlEncode(encoded)); sb.Append("SAMLRequest="); sb.Append(encodedEscaped); this.SignRequest(sb); var result = authnRequest.Destination + "?" + sb.ToString(); return(new Uri(result)); } }
public static string Serialize(object o) { var xmlSerialiser = new XMLSerialiser(); xmlSerialiser.XmlNamespaces.Add("samlp", Saml20Constants.Protocol); xmlSerialiser.XmlNamespaces.Add("saml", Saml20Constants.Assertion); using (var ms = new MemoryStream()) { xmlSerialiser.Serialize(ms, new[] { o }); ms.Position = 0; var streamReader = new StreamReader(ms); var xmlString = streamReader.ReadToEnd(); return(xmlString); } }