Sustainsys / Saml2

Saml2 Authentication services for ASP.NET
Other
961 stars 603 forks source link

How can I generate service profider metadata XML from an SpSsoDescriptor object? #1389

Closed mapitman closed 1 year ago

mapitman commented 1 year ago

I'm trying to generate a Service provider Metadata XML file. My initial thought was I could build up the metadata by creating an SpSsoDescriptor and then somehow serialize it to XML. I did some digging through the object models and also the tests, but nothing jumps out at me and I don't see any examples.

Am I on the right track or is there some other way to do this?

AndersAbel commented 1 year ago

The library automatically generates a service provider Metadata XML file on the path /Saml2.

If you want to do it yourself, you can look in the v2 branch for the code that does that. The current develop branch does not contain that functionality yet.

mapitman commented 1 year ago

We're using the SustainSys.Saml2 library directly in our own SSO implementation, so we don't have the stuff that's setup by the AspNetCore2 library.

I did figure out how to do it myself after I posted this question. I ended up creating an instance of SpSsoDescriptor, and populating it with the required info. Then I created an EntityDescriptor and added the SpSsoDescriptor to the RoleDescriptors collection:

var spDescriptor = new SpSsoDescriptor();
// Set desired property values
var entityDescriptor = new EntityDescriptor();
entityDescriptor.RoleDescriptors.Add(spDescriptor);
entityDescriptor.EntityId = new EntityId(spDescriptor.Id);
// Load a X509Certificate2 from somewhere
var result = entityDescriptor.ToXmlString(signingCertificate, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");

Hopefully this helps someone else in the future!