// // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to You under the Apache License, Version 2.0 // (the "License"); you may not use this file except in compliance with // the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Claims; using System.IdentityModel.Tokens; using System.Xml; namespace Trade.ActiveStsImplementation { public class CustomUsernameTokenHandler : WindowsUserNameSecurityTokenHandler { public override Microsoft.IdentityModel.Claims.ClaimsIdentityCollection ValidateToken(System.IdentityModel.Tokens.SecurityToken token) { UserNameSecurityToken untoken = token as UserNameSecurityToken; if(untoken == null){ throw new SecurityTokenException("Invalid token"); } Claim nameClaim = new Claim(System.IdentityModel.Claims.ClaimTypes.Name, untoken.UserName); IClaimsIdentity ident = new ClaimsIdentity(new List { nameClaim }); ident.Claims.Add( new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationinstant", XmlConvert.ToString(DateTime.UtcNow, "yyyy-MM-ddTHH:mm:ss.fffZ"), "http://www.w3.org/2001/XMLSchema#dateTime") ); ident.Claims.Add( new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/authenticationmethod", "http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/password") ); return new ClaimsIdentityCollection(new IClaimsIdentity[] { ident }); } } }