static async Task Main(string[] args)
        {
            IdentityModelEventSource.ShowPII = true;

            await Task.Delay(5000);

            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()),
                new Claim(ClaimTypes.Name, "username")
            };
            var identity = new ClaimsIdentity(claims, "Sample");
            var handler  = new Saml2SecurityTokenHandler();

            var parameters = CreateRsaParameters();

            using var rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(parameters);

            var descriptor = new SecurityTokenDescriptor
            {
                Issuer             = "urn:sample.identityprovider",
                Audience           = "urn:sample.issuer",
                IssuedAt           = DateTime.UtcNow,
                NotBefore          = DateTime.UtcNow.AddMinutes(-5),
                Expires            = DateTime.UtcNow.AddHours(2),
                Subject            = identity,
                SigningCredentials = SignatureMethod.RsaSha256.CreateCredentials(new RsaSecurityKey(rsa))
            };
            var token = handler.CreateToken(descriptor);

            var binding  = new WsTrustIssuedTokenBinding();
            var endpoint = new EndpointAddress("https://localhost:5001/trust/13");

            var factory = new WsTrustChannelFactory(binding, endpoint);

            factory.SecurityTokenHandlers.Add(handler);

            for (var i = 0; i < iterations; i++)
            {
                var channel = factory.CreateChannelWithIssuedToken(token);

                var request = new WsTrustRequest(WsTrustConstants.Trust13.WsTrustActions.Issue)
                {
                    KeyType   = WsTrustKeyTypes.Trust13.Bearer,
                    AppliesTo = new AppliesTo(new EndpointReference("urn:sample:relyingparty"))
                };
                var response = await channel.IssueAsync(request);

                var requestedToken = response.GetRequestedSecurityToken() as GenericXmlSecurityToken;

                var assertion = requestedToken.Element.OuterXml;
                Console.WriteLine(assertion);
            }
            Console.ReadKey();
        }
        protected override ChannelFactory <TChannel> CreateChannelFactory <TChannel>(Binding binding, EndpointAddress endpointAddress, SoapChannelCreationContext context)
        {
            var factory = new WsTrustChannelFactory(binding, endpointAddress);

            factory.TrustVersion = WsTrustVersion.Trust13;
            if (context.Properties.TryGetValue("handler", out var handler))
            {
                var other = factory.SecurityTokenHandlers.FirstOrDefault(h => h.GetType() == handler.GetType());
                if (other != null)
                {
                    factory.SecurityTokenHandlers.Remove(other);
                }
                factory.SecurityTokenHandlers.Add(handler as SecurityTokenHandler);
            }
            return(factory as ChannelFactory <TChannel>);
        }