ASP.NET 페이지에서 클레임에 액세스하는 방법

WIF(Windows® Identity Foundation)에서는 디자인 타임 컨트롤 및 WS-FAM(WS-Federated 인증 모듈) 프로그래밍 모델을 제공하므로 ASP.NET 개발자는 ASP.NET 페이지의 호출자가 보낸 토큰을 수락할 수 있습니다. 이 토큰에는 WIF가 개발자에게 클레임으로 노출하는 호출자에 대한 정보가 들어 있습니다. 이 항목에서는 이러한 클레임에 액세스하는 방법을 보여 줍니다.

클레임에 액세스하려면

FedUtil을 실행하여 ID 관련 정보에 액세스할 수 있습니다. 자세한 내용은 FedUtil을 사용하여 ASP.NET 신뢰 당사자 응용 프로그램에서 STS로의 트러스트 설정를 참조하십시오. 다음 코드 예제에서 보듯이 FedUtil을 실행하면 응용 프로그램이 표준 ASP.NET 구문을 사용하여 IClaimsPrincipalIClaimsIdentity에 액세스할 수 있습니다.

void Page_Load(object sender, EventArgs e) { // Cast the Thread.CurrentPrincipal IClaimsPrincipal icp = Thread.CurrentPrincipal as IClaimsPrincipal;

    // Access IClaimsIdentity which contains claims IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;
    
    // Access claims foreach(Claim claim in claimsIdentity.Claims) {

    } }

사용자의 클레임을 열거하려면

IClaimsIdentity에 액세스한 경우 해당 클레임 컬렉션을 반복하여 클레임을 열거할 수 있습니다. 다음 코드 샘플은 WIF가 들어오는 보안 토큰에서 추출하는 모든 클레임 속성(즉, 클레임 유형, 클레임 값 및 클레임 값 형식)을 보여 줍니다. 샘플 디렉터리에서 Getting Started/Simple Claims Aware Web Application 샘플도 참조하십시오.

참고

다음 샘플 코드는 프로덕션 환경에서는 사용하지 마십시오. 프로덕션 코드에서는 클레임의 속성을 클라이언트에게 표시하면 보안에 어떤 영향이 있는지를 신중하게 고려해야 합니다. 예를 들어 신뢰 당사자 응용 프로그램에 필요한 클레임 유형만 수락하고, 사용하기 전에 클레임 속성에서 불필요한 부분을 제거하고, 중요한 개인 정보가 포함된 클레임을 필터링하는 것을 고려해야 합니다.

void Page_Load(object sender, EventArgs e) { // Cast the Thread.CurrentPrincipal IClaimsPrincipal icp = Thread.CurrentPrincipal as IClaimsPrincipal;

    // Access IClaimsIdentity which contains claims IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;
    
    // Access claims foreach(Claim claim in claimsIdentity.Claims) { Response.Write(claim.ClaimType) + "<BR>"; Response.Write(claim.Value) + "<BR>"; Response.Write(claim.ValueType) + "<BR>"; } }

특정 클레임에 액세스하려면

IClaimsIdentity에 액세스한 경우 Claims 컬렉션에서 지정된 클레임 유형을 찾아 특정 클레임에 액세스할 수 있습니다. 이 작업은 컬렉션에 있는 클레임을 반복하거나 LINQ를 사용하여 수행할 수 있습니다.

다음 코드 샘플은 컬렉션에 있는 클레임을 반복하여 특정 클레임에 액세스하는 방법을 보여 줍니다.

void Page_Load(object sender, EventArgs e) { // Cast the Thread.CurrentPrincipal IClaimsPrincipal icp = User as IClaimsPrincipal;

    // Access IClaimsIdentity which contains claims IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;
    
    // Access claims foreach(Claim claim in claimsIdentity.Claims) { if(claim.ClaimType == "http://GenevaFramework/AgeClaim") { Response.Write("Age Claim: " + claim.Value); break; } } }

다음 코드 샘플은 LINQ를 사용하여 특정 클레임에 액세스하는 방법을 보여 줍니다.

void Page_Load(object sender, EventArgs e) { // Cast the Thread.CurrentPrincipal IClaimsPrincipal icp = User as IClaimsPrincipal;

    // Access IClaimsIdentity which contains claims IClaimsIdentity claimsIdentity = (IClaimsIdentity)icp.Identity;
    
    // Access claim string ageClaimValue;

    try { ageClaimValue = ( from c in claimsIdentity.Claims where c.ClaimType == "http://GenevaFramework/AgeClaim" select c.Value ).Single(); } catch (InvalidOperationException) { ageClaimValue = "Age claim wasn’t found or " + "there were more than one Age claims provided"; }

    Response.Write("Age Claim: " + ageClaimValue); }