public class EntityDescriptor : SignableMetadataSaml2Object
Shows how to create Metadata (EntityDescriptor) for an IdP.
using ComponentPro.Saml2.Metadata; using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.Xml; ... // Create a new instance of the EntityDescriptor class. EntityDescriptor entityDescriptor = new EntityDescriptor(); // Set ID. entityDescriptor.Id = "MPCSHKBKAJTWEF5RsrHcS2.R3Fb"; // Create a new instance of the AttributeAuthorityDescriptor class. AttributeAuthorityDescriptor attributeAuthorityDescriptor = new AttributeAuthorityDescriptor(); // Add that AttributeAuthorityDescriptor to the entity descriptor. entityDescriptor.AttributeAuthorityDescriptors.Add(attributeAuthorityDescriptor); // Set binding type and location. AttributeService attributeService = new AttributeService(); attributeService.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP"; attributeService.Location = "https://xxx.xxxx.xxxx.com/idp/attrsvc.ssaml2"; attributeAuthorityDescriptor.AttributeServices.Add(attributeService); // Load the key to sign X509Certificate2 x509Certificate = new X509Certificate2(@"Pkey.pfx", "password"); IdpSsoDescriptor ssoDescriptor = new IdpSsoDescriptor(); SingleLogoutService slo = new SingleLogoutService(); slo.Location = "https://xxx.xxxx.xxxx.com/idp/SLO.saml2"; ssoDescriptor.SingleLogoutServices.Add(slo); SingleSignOnService sso = new SingleSignOnService(); sso.Location = "https://xxx.xxxx.xxxx.com/idp/SSO.saml2"; ssoDescriptor.SingleSignOnServices.Add(sso); entityDescriptor.IdpSsoDescriptors.Add(ssoDescriptor); ContactPerson person = new ContactPerson(); person.Company = "company"; person.EmailAddresses.Add("a@email.com"); person.GivenName = "Jenna"; entityDescriptor.ContactPeople.Add(person); // You can add more contact person here... #region Signing key descriptor if needed // Load certificate to sign KeyInfoX509Data certKeyInfoX509Data = new KeyInfoX509Data(x509Certificate); KeyInfo certKeyInfo = new KeyInfo(); certKeyInfo.AddClause(certKeyInfoX509Data); // Add key descriptor KeyDescriptor keyDesc = new KeyDescriptor(); keyDesc.Use = "signing"; keyDesc.KeyInfo = certKeyInfo.GetXml(); ssoDescriptor.KeyDescriptors.Add(keyDesc); #endregion //ssoDescriptor.Sign(x509Certificate); entityDescriptor.Sign(x509Certificate); // Sign the entity descriptor if needed string xml = entityDescriptor.GetXml().OuterXml; System.Diagnostics.Trace.WriteLine(xml);
Shows how to create Metadata (EntityDescriptor) for an SP.
using ComponentPro.Saml2.Metadata; using System.Xml; using System.Security.Cryptography.Xml; using System.Security.Cryptography.X509Certificates; ... // Load your certificate. X509Certificate2 x509Certificate = new X509Certificate2(@"..\..\Pkey.pfx", "password"); // Create Entity Descriptor with ID received from the IdP. EntityDescriptor descriptor = new EntityDescriptor(); descriptor.Id = "84CCAA9F05EE4BA1B13F8943FDF1D320"; SpSsoDescriptor spd = new SpSsoDescriptor(); spd.Id = "someid"; spd.AuthnRequestsSigned = true; spd.ProtocolSupportEnumeration = "urn:oasis:names:tc:SAML:2.0:protocol"; // Creating a key descriptor. KeyDescriptor keyDescriptor = new KeyDescriptor(); keyDescriptor.Use = "signing"; KeyInfoX509Data keyData = new KeyInfoX509Data(x509Certificate); // Create KeyInfo. KeyInfo keyInfo = new KeyInfo(); keyInfo.AddClause(keyData); keyDescriptor.KeyInfo = keyInfo.GetXml(); // Add KeyDescriptor. spd.KeyDescriptors.Add(keyDescriptor); // Assign assertion service url. AssertionConsumerService consumerService = new AssertionConsumerService(); consumerService.Index = 0; consumerService.IsDefault = true; consumerService.Location = "http://www.test.com/AssertionService.aspx"; consumerService.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"; spd.AssertionConsumerServices.Add(consumerService); descriptor.SpSsoDescriptors.Add(spd); // Add some information. // Organization information descriptor.Organization = new Organization(); descriptor.Organization.OrganizationNames.Add(new OrganizationName("Company - some name", "en")); descriptor.Organization.OrganizationDisplayNames.Add(new OrganizationDisplayName("Company", "en")); descriptor.Organization.OrganizationUrls.Add(new OrganizationUrl("https://www.company.be", "en")); // Add contact person info. ContactPerson person = new ContactPerson(); person.Company = "Company"; person.EmailAddresses.Add("helpdesk@company.be"); // Contact information descriptor.ContactPeople.Add(person); // Sign metadata with service provider key descriptor.Sign(x509Certificate); // Get XML element and its content. XmlElement xml = descriptor.GetXml(); // Print out System.Diagnostics.Trace.WriteLine(xml.OuterXml); //XmlDocument document = xml.OwnerDocument; //context.Response.ContentType = "text/xml"; //context.Response.ContentEncoding = System.Text.Encoding.UTF8; //document.Save(context.Response.Output); //context.Response.End();
Shows how to create Metadata.
using ComponentPro.Saml2.Metadata; using System.Security.Cryptography.X509Certificates; ... // Create a new instance of the EntityDescriptor class. EntityDescriptor entityDescriptor = new EntityDescriptor(); // Set ID. entityDescriptor.Id = "MPCSHKBKAJTWEF5RsrHcS2.R3Fb"; // Create a new instance of the AttributeAuthorityDescriptor class. AttributeAuthorityDescriptor attributeAuthorityDescriptor = new AttributeAuthorityDescriptor(); // Add that AttributeAuthorityDescriptor to the entity descriptor. entityDescriptor.AttributeAuthorityDescriptors.Add(attributeAuthorityDescriptor); // Set binding type and location. AttributeService attributeService = new AttributeService(); attributeService.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP"; attributeService.Location = "https://xxx.xxxx.xxxx.com/idp/attrsvc.ssaml2"; attributeAuthorityDescriptor.AttributeServices.Add(attributeService); // Load the key to sign X509Certificate2 x509Certificate = new X509Certificate2(@"\..\..\Pkey.pfx", "password"); SpSsoDescriptor ssoDescriptor = new SpSsoDescriptor(); ArtifactResolutionService ars = new ArtifactResolutionService(); ars.IsDefault = true; ars.Location = "https://ssoqa.demodomain.com/hsyeg/sp/ARS.ssaml2"; ssoDescriptor.ArtifactResolutionServices.Add(ars); SingleLogoutService slo = new SingleLogoutService(); slo.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"; slo.Location = "https://ssoqa.demodomain.com/hsyeg/sp/SLO.saml2"; ssoDescriptor.SingleLogoutServices.Add(slo); slo = new SingleLogoutService(); slo.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"; slo.Location = "https://ssoqa.demodomain.com/hsyeg/sp/SLO.saml2"; ssoDescriptor.SingleLogoutServices.Add(slo); slo = new SingleLogoutService(); slo.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact"; slo.Location = "https://ssoqa.demodomain.com/hsyeg/sp/SLO.saml2"; ssoDescriptor.SingleLogoutServices.Add(slo); slo = new SingleLogoutService(); slo.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP"; slo.Location = "https://ssoqa.demodomain.com/hsyeg/sp/SLO.ssaml2"; ssoDescriptor.SingleLogoutServices.Add(slo); AssertionConsumerService acs = new AssertionConsumerService(); acs.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"; acs.Location = "https://ssoqa.demodomain.com/hsyeg/sp/ACS.saml2"; ssoDescriptor.AssertionConsumerServices.Add(acs); AttributeConsumingService attcs = new AttributeConsumingService(); attcs.ServiceNames.Add(new ServiceName("AttributeContract", "en")); attcs.RequestedAttributes.Add(new RequestedAttribute("lname")); attcs.RequestedAttributes.Add(new RequestedAttribute("mid")); attcs.RequestedAttributes.Add(new RequestedAttribute("fname")); ssoDescriptor.AttributeConsumingServices.Add(attcs); // You may want to sign // ssoDescriptor.Sign(x509Certificate); entityDescriptor.SpSsoDescriptors.Add(ssoDescriptor); ContactPerson person = new ContactPerson(); person.Company = "Demo Domain"; person.GivenName = "John"; person.Surname = "Brown"; person.EmailAddresses.Add("a@email.com"); person.TelephoneNumbers.Add("12345"); entityDescriptor.ContactPeople.Add(person); //RoleDescriptor role = new RoleDescriptor(); //KeyDescriptor key = new KeyDescriptor(); //key.Use = "encryption"; //role.KeyDescriptors.Add(key); //ssoDescriptor.Sign(x509Certificate); // Sign SSO Descriptor if needed. entityDescriptor.Sign(x509Certificate); // In this case we sign the entity descriptor. string xml = entityDescriptor.GetXml().OuterXml; System.Diagnostics.Trace.WriteLine(xml);