Пример #1
0
        private void CreateSamlTokens(SecurityTokenDescriptor tokenDescriptor)
        {
            SamlSecurityTokenHandler samlTokenHandler = new SamlSecurityTokenHandler();
            SamlSecurityToken        token            = samlTokenHandler.CreateToken(tokenDescriptor) as SamlSecurityToken;
            MemoryStream             ms     = new MemoryStream();
            XmlDictionaryWriter      writer = XmlDictionaryWriter.CreateTextWriter(ms);

            samlTokenHandler.WriteToken(writer, token);
        }
        private static string CreateSignedXmlWithEmbededTokens(IList <SecurityToken> samlTokens, SigningCredentials xmlSigningCredentials, SigningCredentials tokenSigningCredentials)
        {
            var ms                = new MemoryStream();
            var writer            = XmlDictionaryWriter.CreateTextWriter(ms, Encoding.UTF8, false);
            var samlTokenHandler  = new SamlSecurityTokenHandler();
            var saml2TokenHandler = new Saml2SecurityTokenHandler();
            var envelopedWriter   = new EnvelopedSignatureWriter(writer, xmlSigningCredentials, "ref#1");

            envelopedWriter.WriteStartElement("local", "elementName", "http://elementnamespace");
            envelopedWriter.WriteElementString("localElement", "SamlWillBeEmbeded");

            foreach (var token in samlTokens)
            {
                if (token is SamlSecurityToken)
                {
                    samlTokenHandler.WriteToken(envelopedWriter, token);
                }
                else
                {
                    saml2TokenHandler.WriteToken(envelopedWriter, token);
                }
            }

            envelopedWriter.WriteStartElement("local", "elementName2", "http://elementnamespace");
            envelopedWriter.WriteElementString("localElement", "SamlWillBeEmbeded2");
            foreach (var token in samlTokens)
            {
                if (token is SamlSecurityToken)
                {
                    samlTokenHandler.WriteToken(envelopedWriter, token);
                }
                else
                {
                    saml2TokenHandler.WriteToken(envelopedWriter, token);
                }
            }

            envelopedWriter.WriteEndElement();
            envelopedWriter.WriteEndElement();
            envelopedWriter.Flush();
            var xml = Encoding.UTF8.GetString(ms.ToArray());

            return(xml);
        }
        /// <summary>
        /// Requests an SWT Token using an input SAML token.
        /// </summary>
        /// <param name="token">The input SAML token.</param>
        /// <param name="scope">The requested scope.</param>
        /// <returns>The requested SWT token</returns>
        public SimpleWebToken Issue(SamlSecurityToken token, Uri scope)
        {
            var handler = new SamlSecurityTokenHandler();

            var sb = new StringBuilder(128);

            handler.WriteToken(new XmlTextWriter(new StringWriter(sb)), token);

            return(IssueAssertion(sb.ToString(), "SAML", scope));
        }
        public string IssueTokenAsXml(ClaimsPrincipal principal, string appliesTo)
        {
            var token  = IssueToken(principal, appliesTo);
            var buffer = new StringBuilder();

            var handler = new SamlSecurityTokenHandler
            {
                Configuration = new SecurityTokenHandlerConfiguration()
            };
            var writer = XmlWriter.Create(new StringWriter(buffer));

            handler.WriteToken(writer, token);
            var tokenXml = buffer.ToString();

            return(tokenXml);
        }
Пример #5
0
        public MeasureTokenHandlers()
        {
            IdentityModelEventSource.ShowPII = true;
            var securityTokenDescriptor = new SecurityTokenDescriptor
            {
                Audience           = TestData.Audience,
                Claims             = TestData.ClaimsDictionary,
                Issuer             = TestData.Issuer,
                Subject            = TestData.Subject,
                SigningCredentials = TestData.RsaSigningCredentials_2048Sha256
            };

            _jwtToken   = _jsonWebTokenHandler.CreateToken(securityTokenDescriptor);
            _saml1Token = _saml1SecurityTokenHandler.WriteToken(_saml1SecurityTokenHandler.CreateToken(securityTokenDescriptor));
            _saml2Token = _saml2SecurityTokenHandler.WriteToken(_saml2SecurityTokenHandler.CreateToken(securityTokenDescriptor));
            _tokenValidationParameters = TestData.RsaTokenValidationParameters_2048_Public;
        }
Пример #6
0
        public static string WriteSamlToken(SecurityToken token)
        {
            using (var stringWriter = new StringWriter())
                using (var writer = XmlWriter.Create(stringWriter))
                {
                    if (writer == null)
                    {
                        throw new Exception("Could not create XMlWriter based on empty stringwriter");
                    }

                    var handler = new SamlSecurityTokenHandler();
                    handler.WriteToken(writer, token);

                    writer.Flush();
                    var tokenString = stringWriter.ToString();
                    return(tokenString);
                }
        }
Пример #7
0
        private static void AddWaSignInVariation(IList <Claim> claims, string variation, TheoryData <WsFederationSigninMessageTheoryData> theoryData)
        {
            var samlToken = CreateSamlToken(claims);
            var samlSecurityTokenHandler = new SamlSecurityTokenHandler();

            theoryData.Add(new WsFederationSigninMessageTheoryData
            {
                QueryString          = WsFederationTestUtilities.BuildWaSignInMessage(samlToken, samlSecurityTokenHandler, "saml1" + variation),
                SecurityToken        = samlToken,
                SecurityTokenHandler = samlSecurityTokenHandler,
                TestId = "Saml1WriteToken" + variation
            });

            theoryData.Add(new WsFederationSigninMessageTheoryData
            {
                QueryString          = WsFederationTestUtilities.BuildWaSignInMessage(samlSecurityTokenHandler.WriteToken(samlToken), "saml1" + variation),
                SecurityToken        = samlToken,
                SecurityTokenHandler = samlSecurityTokenHandler,
                TestId = "Saml1SetToken" + variation
            });

            // this results in %0D in the query string
            var saml = samlSecurityTokenHandler.WriteToken(samlToken).Replace("&#xD;", "\r");

            theoryData.Add(new WsFederationSigninMessageTheoryData
            {
                QueryString          = WsFederationTestUtilities.BuildWaSignInMessage(saml, "saml1" + variation),
                SecurityToken        = samlToken,
                SecurityTokenHandler = samlSecurityTokenHandler,
                TestId = "Saml1SetTokenReplace" + variation
            });

            var saml2Token = CreateSaml2Token(claims);
            var saml2SecurityTokenHandler = new Saml2SecurityTokenHandler();

            theoryData.Add(new WsFederationSigninMessageTheoryData
            {
                QueryString          = WsFederationTestUtilities.BuildWaSignInMessage(saml2Token, saml2SecurityTokenHandler, "saml2" + variation),
                SecurityToken        = saml2Token,
                SecurityTokenHandler = saml2SecurityTokenHandler,
                TestId = "Saml2WriteToken" + variation
            });

            theoryData.Add(new WsFederationSigninMessageTheoryData
            {
                QueryString          = WsFederationTestUtilities.BuildWaSignInMessage(saml2SecurityTokenHandler.WriteToken(saml2Token), "saml2" + variation),
                SecurityToken        = saml2Token,
                SecurityTokenHandler = saml2SecurityTokenHandler,
                TestId = "Saml2SetToken" + variation
            });

            // this results in %0D in the query string
            var saml2 = saml2SecurityTokenHandler.WriteToken(saml2Token).Replace("&#xD;", "\r");

            theoryData.Add(new WsFederationSigninMessageTheoryData
            {
                QueryString          = WsFederationTestUtilities.BuildWaSignInMessage(saml2, "saml2" + variation),
                SecurityToken        = saml2Token,
                SecurityTokenHandler = saml2SecurityTokenHandler,
                TestId = "Saml2SetTokenReplace" + variation
            });
        }
        static string CreateSamlTokenString()
        {
            var tokenHandler = new SamlSecurityTokenHandler();

            return(tokenHandler.WriteToken(CreateSamlToken()));
        }
        public static void Run(string[] args)
        {
            IdentityModelEventSource.ShowPII = true;
            var testRuns = TestConfig.SetupTestRuns(
                new List <TestExecutor>
            {
                TokenTestExecutors.JsonWebTokenHandler_ValidateToken_InParallel,
                TokenTestExecutors.JwtSecurityTokenHandler_ValidateToken_InParallel,
                TokenTestExecutors.Saml2SecurityTokenHandler_ValidateToken_InParallel,
                TokenTestExecutors.SamlSecurityTokenHandler_ValidateToken_InParallel,
                TokenTestExecutors.JsonWebTokenHandler_CreateToken_InParallel,
                TokenTestExecutors.JwtSecurityTokenHandler_CreateToken_InParallel,
                TokenTestExecutors.Saml2SecurityTokenHandler_CreateToken_InParallel,
                TokenTestExecutors.SamlSecurityTokenHandler_CreateToken_InParallel,
            });

            var securityTokenDescriptor   = TestData.SecurityTokenDescriptor(TestData.RsaSigningCredentials_2048Sha256);
            var tokenValidationParameters = TestData.TokenValidationParameters(securityTokenDescriptor.SigningCredentials.Key);
            var jwtTokenHandler           = new JwtSecurityTokenHandler();
            var jwt = jwtTokenHandler.CreateEncodedJwt(securityTokenDescriptor);
            var samlTokenHandler  = new SamlSecurityTokenHandler();
            var samlToken         = samlTokenHandler.CreateToken(securityTokenDescriptor);
            var saml              = samlTokenHandler.WriteToken(samlToken);
            var saml2TokenHandler = new Saml2SecurityTokenHandler();
            var saml2Token        = saml2TokenHandler.CreateToken(securityTokenDescriptor);
            var saml2             = saml2TokenHandler.WriteToken(saml2Token);

            var testConfig    = TestConfig.ParseArgs(args);
            var tokenTestData = new TokenTestRunData
            {
                JwtSecurityTokenHandler = new JwtSecurityTokenHandler(),
                JsonWebTokenHandler     = new JsonWebTokenHandler(),
                JwtToken                  = jwt,
                NumIterations             = testConfig.NumIterations,
                Saml2Token                = saml2,
                SamlToken                 = saml,
                SamlSecurityTokenHandler  = samlTokenHandler,
                Saml2SecurityTokenHandler = saml2TokenHandler,
                TokenValidationParameters = tokenValidationParameters,
                SecurityTokenDescriptor   = securityTokenDescriptor
            };

            // run each test to set any static data
            foreach (var testRun in testRuns)
            {
                testRun.TestExecutor(tokenTestData);
            }

            var assemblyVersion = typeof(JwtSecurityTokenHandler).Assembly.GetName().Version.ToString();

#if DEBUG
            var prefix = "DEBUG";
#else
            var prefix = "RELEASE";
#endif
            testConfig.Version = $"{prefix}-{assemblyVersion}";
            var logName   = $"SecurityTokens-{testConfig.Version}_{DateTime.Now.ToString("yyyy.MM.dd.hh.mm.ss")}.txt";
            var directory = testConfig.LogDirectory;
            var logFile   = Path.Combine(directory, logName);
            Directory.CreateDirectory(directory);

            TestRunner.Run(testConfig, testRuns, tokenTestData);
            File.WriteAllText(logFile, testConfig.Logger.Logs);
        }