saml-2.0itfoxtec-identity-saml2

How do you transform SAML 2 claims to readable values?


I am using the ITFoxTec SAML 2.0 library in a .Net Core 2 web app.

When my app authenticates with an IdP, I am trying to get the email address back.

So I wrote out the contents of: saml2AuthnResponse.ClaimsIdentity

I see the email in there, but it's a odd long string that looks like this: urn:oid:1.3.6.1.4.1.5923.1.1.1.6: James.Iha@sp.edu

How do I get the just the values like last name, email, etc... without all the other stuff that comes along with it?

Thanks!


Solution

  • You iterate over the claims for the claimsidentity; that gives you the properties for each of the claims in separate fields. To get just the email:

    var email = saml2AuthnResponse.ClaimsIdentity.Claims.FirstOrDefault(c => c.Type == "urn:oid:1.3.6.1.4.1.5923.1.1.1.6")?.Value;
    

    to enumerate all claims and their values:

    foreach (var claim in saml2AuthnResponse.ClaimsIdentity.Claims)
    {
        Console.WriteLine(claim.Type);
        Console.WriteLine(claim.Value);
    }