Tuesday, March 26, 2013

Parse claim from STS in .net 4.5

When creating an asp.net 4.5 web application that is acting as an RP to an STS system, you may be fine checking the User.IsInRole("WtEv") functionality provided from WIF. If you find yourself needing to parse an individual value for some other reason, the code is a bit more sketchy. Following is a snippet to parse individual claims from a ClaimsPrinicpal inside of an RP using STS.

ClaimsPrincipal claimsPrincipal = HttpContext.Current.User as ClaimsPrincipal;
if (claimsPrincipal != null && claimsPrincipal.Identity.IsAuthenticated)
{
  try
  {
       string CustomerID = (from c in claimsPrincipal.Claims where 
           c.Type == "http://devstorm.blogspot.com/claims/CustomerID" 
           select c.Value).Single(); 
 
       string Protocol = (from c in claimsPrincipal.Claims where 
           c.Type == "http://devstorm.blogspot.com/claims/Protocol" 
           select c.Value).Single(); 
 
       string ApplicationServer = (from c in claimsPrincipal.Claims where 
           c.Type == "http://devstorm.blogspot.com/claims/ApplicationServer" 
           select c.Value).Single();
  }
  catch (InvalidOperationException)
  {
    // handle claims not existing
  }
} 

No comments:

Post a Comment