사용자 지정 토큰 처리기를 추가하는 방법

SecurityTokenHandler 또는 해당 하위 항목 중 하나에서 파생되는 클래스를 만든 다음 이 클래스를 응용 프로그램의 web.config 또는 app.config 파일에 추가하는 방식으로 쉽게 사용자 지정 보안 토큰 처리기를 추가할 수 있습니다. SAML 2 또는 X.509와 같이 잘 알려진 토큰 유형을 처리하기 위해 사용자 지정 보안 토큰 처리기를 작성하는 경우 적절한 WIF 클래스에서 파생된 클래스를 만들어야 합니다. 예를 들어 SAML 2 토큰을 처리하려면 Saml2SecurityTokenHandler에서 파생된 클래스를 만들어야 합니다. 이 항목에서는 사용자 지정 사용자 이름/암호 유효성 검사를 제공하는 사용자 지정 보안 토큰 처리기를 추가하는 방법을 보여 줍니다.

먼저 UserNameSecurityTokenHandler에서 파생되는 클래스를 만든 다음 CanValidateTokenValidateToken을 재정의합니다.

using Microsoft.IdentityModel.SecurityTokenService; using Microsoft.IdentityModel.Tokens;

namespace SimpleCustomSecurityTokenHandler { public class CustomUserNamePasswordValidatorSecurityTokenHandler : UserNameSecurityTokenHandler { /* Important: You must override CanValidateToken and return true, or your token handler will not be used. */ public override bool CanValidateToken { get { return true; } }

        public override ClaimsIdentityCollection ValidateToken(SecurityToken token) { if (token == null) { throw new ArgumentNullException(); } UserNameSecurityToken UNtoken = token as UserNameSecurityToken; if (UNtoken == null) { throw new SecurityTokenException("Invalid token"); }

/* Validate UNtoken.UserName and UNtoken.Password here. */

            Claim claim = new Claim(System.IdentityModel.Claims.ClaimTypes.Name, UNtoken.UserName); IClaimsIdentity identity = new ClaimsIdentity(nameClaim,"Password"); identity.Claims.Add( new Claim("https://schemas.microsoft.com/ws/2008/06/identity/claims/ClaimTypes.AuthenticationInstant", XmlConvert.ToString(DateTime.UtcNow, "yyyy-MM-ddTHH:mm:ss.fffZ"), "http://www.w3.org/2001/XMLSchema#dateTime") ); identity.Claims.Add( new Claim("https://schemas.microsoft.com/ws/2008/06/identity/claims/ClaimTypes.AuthenticationMethod", "https://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/password") ); return new ClaimsIdentityCollection(new IClaimsIdentity[] { identity }); } } }

다음으로, 다음 태그를 서비스의 app.config 파일에 있는 configuration 섹션에 추가하는 방식으로 서비스 구성에 사용자 지정 토큰 처리기를 추가합니다.

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <!-- Registers the microsoft.IdentityModel configuration section --> <section name="microsoft.identityModel" type="Microsoft.IdentityModel.Configuration.MicrosoftIdentityModelSection, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </configSections> <microsoft.identityModel> <service> <securityTokenHandlers> <remove type="Microsoft.IdentityModel.Tokens.WindowsUserNameSecurityTokenHandler, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add type="SimpleCustomSecurityTokenHandler.CustomUserNamePasswordValidatorSecurityTokenHandler, SimpleCustomSecurityTokenHandler"/> </securityTokenHandlers> </service> </microsoft.identityModel> </configuration>

사용자 지정 보안 토큰 처리기인 CustomUserNamePasswordValidatorSecurityTokenHandler를 추가할 뿐만 아니라 기본 사용자 이름/암호 보안 토큰 처리기인 WindowsUserNameSecurityTokenHandler도 제거한다는 점에 유의하십시오. 이렇게 하는 이유는 WIF의 토큰 처리기 컬렉션이 기본적으로 각 토큰 유형에 대해 하나의 토큰 처리기만 포함하기 때문입니다. 토큰 처리기와 토큰 유형 사이에는 일대일 관계가 있으므로 둘 이상의 토큰 유형을 처리하는 사용자 지정 토큰 처리기는 작성하지 않아야 합니다. 토큰 유형은 하나 이상의 토큰 식별자 문자열로 표현할 수 있으므로 하나의 토큰 유형과 둘 이상의 토큰 식별자를 처리하는 사용자 지정 토큰 처리기를 작성할 수 있습니다.

사용자 지정 보안 토큰 처리기가 ReadToken을 재정의하는 경우에는 CanReadToken도 재정의해야 합니다. 마찬가지로 사용자 지정 보안 토큰 처리기가 WriteToken을 재정의하는 경우에는 CanWriteToken도 재정의해야 합니다.