Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AudienceRestriction in SAML Assertion

Tags:

c#

saml

Can someone please point me in the direction of an example creating a SamlAssertion that includes an AudienceRestriction in the Conditions node?

below is an example of my code where I would want to put it:

//Create the SAML Assertion
SamlAssertion samlAssert = new SamlAssertion();
samlAssert.AssertionId = Convert.ToBase64String(encoding.GetBytes(System.Guid.NewGuid().ToString()));
samlAssert.Issuer = "http://www.example.com/";

// Set up the conditions of the assertion - Not Before and Not After
samlAssert.Conditions = new SamlConditions(DateTime.Now, DateTime.Now.AddMinutes(5));

The desired XML looks something like this:

<Assertion xmlns="urn:oasis:names:tc:SAML:1.0:assertion" AssertionID="_e835eca079133299b2f8a2a63ad72fe8" IssueInstant="2007-02-07T20:22:58.165Z" Issuer="http://www.example.com/" MajorVersion="1" MinorVersion="1">
 <Conditions NotBefore="2007-02-07T20:22:58.162Z" NotOnOrAfter="2007-02-07T20:24:58.162Z">
  <AudienceRestrictionCondition>
   <Audience>http://www.example2.com</Audience> 
  </AudienceRestrictionCondition>
 </Conditions>

I see that there's a constructor for SamlConditions class that allows for a 3rd parameter, the conditions, and that there's a SamlAudienceRestriction class, but I can't seem to figure out how to connect the two. I think if I were to see a bit of code, it would become painfully obvious to me, but unfortunately, my google-foo is failing me today.

like image 734
mjmcinto Avatar asked Apr 27 '26 04:04

mjmcinto


1 Answers

I swear I spent several hours trying to figure this one out before posting...but apparently posting was exactly what I needed to see the answer. Below is the code I did to create the audience restriction for the SAML:

//Create the SAML Assertion
SamlAssertion samlAssert = new SamlAssertion();
samlAssert.AssertionId = Convert
    .ToBase64String(
    encoding.GetBytes(System.Guid.NewGuid().ToString()));
samlAssert.Issuer = "http://www.example.com/";

// Set up the conditions of the assertion - Not Before and Not After
Uri[] approvedAudiences = {new Uri("http://www.example2.com")};
List<SamlCondition> conditions = new List<SamlCondition>();
conditions.Add(new SamlAudienceRestrictionCondition(approvedAudiences));
samlAssert.Conditions = new SamlConditions(
    DateTime.Now, 
    DateTime.Now.AddMinutes(5), 
    conditions
    );

If anyone sees anything wrong, or knows of a better/more efficient way, please let me know.

like image 111
mjmcinto Avatar answered Apr 29 '26 17:04

mjmcinto