SamlSecurityToken GetSamlToken () { SamlAssertion a = new SamlAssertion (); SamlSubject subject = new SamlSubject ( SamlConstants.UserNameNamespace, "urn:myqualifier", "myname"); SamlAttribute attr = new SamlAttribute (Claim.CreateNameClaim ("myname")); SamlAttributeStatement statement = new SamlAttributeStatement (subject, new SamlAttribute [] {attr}); a.Statements.Add (statement); a.Issuer = "my_hero"; X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.pfx", "mono"); X509AsymmetricSecurityKey key = new X509AsymmetricSecurityKey (cert); a.SigningCredentials = new SigningCredentials (key, SecurityAlgorithms.HmacSha1Signature, SecurityAlgorithms.Sha256Digest); XmlDocument doc = new XmlDocument (); XmlWriter w = doc.CreateNavigator ().AppendChild (); using (XmlDictionaryWriter dw = XmlDictionaryWriter.CreateDictionaryWriter (w)) { a.WriteXml (dw, new SamlSerializer (), new MySecurityTokenSerializer ()); } Console.Error.WriteLine (doc.OuterXml); return new SamlSecurityToken (a); }
public void WriteXmlValid () { SamlAssertion a = new SamlAssertion (); SamlSubject subject = new SamlSubject ( SamlConstants.UserNameNamespace, "urn:myqualifier", "myname"); SamlAttribute attr = new SamlAttribute (Claim.CreateNameClaim ("myname")); SamlAttributeStatement statement = new SamlAttributeStatement (subject, new SamlAttribute [] {attr}); a.Advice = new SamlAdvice (new string [] {"urn:testadvice1"}); DateTime notBefore = DateTime.SpecifyKind (new DateTime (2000, 1, 1), DateTimeKind.Utc); DateTime notOnAfter = DateTime.SpecifyKind (new DateTime (2006, 1, 1), DateTimeKind.Utc); a.Conditions = new SamlConditions (notBefore, notOnAfter); a.Statements.Add (statement); a.Issuer = "my_hero"; StringWriter sw = new StringWriter (); string id = a.AssertionId; DateTime instant = a.IssueInstant; using (XmlDictionaryWriter dw = CreateWriter (sw)) { a.WriteXml (dw, new SamlSerializer (), null); } string expected = String.Format ("<?xml version=\"1.0\" encoding=\"utf-16\"?><saml:Assertion MajorVersion=\"1\" MinorVersion=\"1\" AssertionID=\"{0}\" Issuer=\"my_hero\" IssueInstant=\"{1}\" xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\"><saml:Conditions NotBefore=\"{3}\" NotOnOrAfter=\"{4}\" /><saml:Advice><saml:AssertionIDReference>urn:testadvice1</saml:AssertionIDReference></saml:Advice><saml:AttributeStatement><saml:Subject><saml:NameIdentifier Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName\" NameQualifier=\"urn:myqualifier\">myname</saml:NameIdentifier></saml:Subject><saml:Attribute AttributeName=\"name\" AttributeNamespace=\"{2}\"><saml:AttributeValue>myname</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion>", id, instant.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture), "http://schemas.xmlsoap.org/ws/2005/05/identity/claims", notBefore.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture), notOnAfter.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture)); Assert.AreEqual (expected, sw.ToString ()); }
public void WriteXmlWithoutSamlSubject () { SamlAssertion a = new SamlAssertion (); a.Statements.Add (new SamlAttributeStatement ()); a.Issuer = "my_boss"; StringWriter sw = new StringWriter (); using (XmlDictionaryWriter dw = CreateWriter (sw)) { a.WriteXml (dw, new SamlSerializer (), null); } Assert.AreEqual ("<?xml version=\"1.0\" ?>", sw.ToString ()); }
public void WriteXmlNullSerializer () { SamlAssertion a = new SamlAssertion (); a.Statements.Add (new SamlAttributeStatement ()); a.Issuer = "my_hero"; using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) { a.WriteXml (dw, null, null); } }
public void WriteXmlNoStatement () { SamlAssertion a = new SamlAssertion (); a.Issuer = "my_boss"; using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) { a.WriteXml (dw, null, null); } }
public void WriteXmlNullIssuer () { SamlAssertion a = new SamlAssertion (); using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) { a.WriteXml (dw, null, null); } }