How to create SAML Metadata XML for ServiceProvider

The Service Provider (SP) receives metadata from the Identity Provider (IdP), parses it and sends back SP Metadata XML to the IdP. The SP Metadata XML contains information of binding location, organization, contact person, etc. It is signed with a private key and the IdP needs a corresponding public key to decrypt it. You can use the following code in conjunction with the code in the previous post to make a connection between your IdP and SP. This makes use of the EntityDescriptor class to construct a response message sending back to the IdP. When the message is needed to be signed, we use the KeyDescriptor class to add X509 certificate's data. The AssertionConsumerService class and some other informative classes such as ContactPerson and Organization are to store user information.

In addition to creating the metadata for Service Provider, Ultimate SAML also helps you create Metadata for Identity Provider.


// 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();
keyDescriptor.KeyInfo = keyInfo.GetXml();
// Add KeyDescriptor.

// Assign assertion service URL.
AssertionConsumerService consumerService = new AssertionConsumerService();
consumerService.Index = 0;
consumerService.IsDefault = true;
consumerService.Location = "";
consumerService.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST";


// Add some information.
// Organization information
descriptor.Organization = new Organization();
    .Add(new OrganizationName("Company - some name", "en"));
    .Add(new OrganizationDisplayName("Company", "en"));
descriptor.Organization.OrganizationUrls.Add(new OrganizationUrl("", "en"));

// Add contact person info.
ContactPerson person = new ContactPerson();
person.Company = "Company";
// Contact information
// Sign metadata with service provider key

// Get XML element and its content.
XmlElement xml = descriptor.GetXml();

// Print out

//XmlDocument document = xml.OwnerDocument;

//context.Response.ContentType = "text/xml";
//context.Response.ContentEncoding = System.Text.Encoding.UTF8;


' Load your certificate.
Dim x509Certificate As New X509Certificate2("..\..\Pkey.pfx", "password")

' Create Entity Descriptor with ID received from the IdP.
Dim descriptor As New EntityDescriptor()
descriptor.Id = "84CCAA9F05EE4BA1B13F8943FDF1D320"
Dim spd As New SpSsoDescriptor()
spd.Id = "someid"
spd.AuthnRequestsSigned = True
spd.ProtocolSupportEnumeration = "urn:oasis:names:tc:SAML:2.0:protocol"

' Creating a signing key.
Dim signingKey As New KeyDescriptor()
signingKey.Use = "signing"
Dim keyData As New KeyInfoX509Data(x509Certificate)
signingKey.KeyInfo = keyData.GetXml()

' Assign assertion service URL.
Dim consumerService As New AssertionConsumerService()
consumerService.Index = 0
consumerService.IsDefault = True
consumerService.Location = ""
consumerService.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"


' 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("", "en"))

' Add contact person info.
Dim person As New ContactPerson()
person.Company = "Company"
' Contact information
' Sign metadata with service provider key

' Get the XML element and its content.
Dim xml As XmlElement = descriptor.GetXml()

' Print out

'XmlDocument document = xml.OwnerDocument;

'context.Response.ContentType = "text/xml";
'context.Response.ContentEncoding = System.Text.Encoding.UTF8;

45-Day Money Back Guarantee

We will refund your full money in 45 days
if you are not satisfied with our products

Buy Now

Dont miss out Get update on new articles and other opportunities