Search code examples
c#samlwifws-federation

how can i access the raw xml of the saml token?


I can get all the claims values with code like this:

ClaimsPrincipal cp = Thread.CurrentPrincipal as ClaimsPrincipal;            
ClaimsIdentity cid = (ClaimsIdentity)cp.Identity;

foreach (Claim claim in cid.Claims)
{
   ...
}

but i want to be able to read the entire token as i suspect a some info i need is in a non-attribute section of the token. I've read about the security token visualizer control, but it's not something i can use because i don't want end users seeing the control output, but would like to send the raw token xml via email.


Solution

  • First you must configure WIF to save the "BootstrapContext":

    <system.identityModel>
      <identityConfiguration saveBootstrapContext="true">
    

    Then you can use the Property "BootstrapContext" on the "ClaimsIdentity"

    ClaimsPrincipal cp = Thread.CurrentPrincipal as ClaimsPrincipal;
    ClaimsIdentity cid = (ClaimsIdentity)cp.Identity; 
    BootstrapContext bc = cid.BootstrapContext as BootstrapContext;
    

    And use the "Token" Property on the BootstrapContext to get the raw xml of the saml token.

    See also:

    https://msdn.microsoft.com/en-us/library/system.security.claims.claimsidentity.bootstrapcontext(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/system.identitymodel.tokens.bootstrapcontext(v=vs.110).aspx

    http://www.cloudidentity.com/blog/2012/11/30/using-the-bootstrapcontext-property-in-net-4-5-2/