static void Main(string[] args)
        {
            SigningCredentials signingCreds = new X509SigningCredentials("CN=MySTS".ToCertificate());

            SecurityTokenServiceConfiguration config =
                new SecurityTokenServiceConfiguration("http://MySTS", signingCreds);

            config.SecurityTokenHandlers.AddOrReplace(new CustomUsernameTokenHandler());
            config.SecurityTokenService = typeof(MySecurityTokenService);

            // Create the WS-Trust service host with our STS configuration
            var host = new WSTrustServiceHost(config, new Uri("http://localhost:6000/MySTS"));

            try
            {
                host.Open();
                Console.WriteLine("STS is ready to issue tokens… Press ENTER to shutdown");
                Console.ReadLine();
                host.Close();
            }
            finally
            {
                if (host.State != CommunicationState.Faulted)
                {
                    host.Close();
                }
                else
                {
                    host.Abort();
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            ServiceHost        serviceHost = null;
            WSTrustServiceHost securityTokenServiceHost = null;

            try
            {
                //
                // Open the calculator service host.
                //
                serviceHost = CreateServiceHost();
                serviceHost.Open();
                Console.WriteLine("Started the calculator service.");
                WriteEndpoints(serviceHost);

                //
                // Start the STS
                //
                securityTokenServiceHost = CreateSecurityTokenServiceHost();
                securityTokenServiceHost.Open();
                Console.WriteLine("Started the STS.");
                WriteEndpoints(securityTokenServiceHost);

                //
                // Call the service and let the framework request the
                // token from the STS automatically.
                //
                Console.WriteLine("Calling the service with an issued token implicitly acquired using WCF...");
                CallService();

                //
                // Use the WSTrustChannel component to manually acquire
                // the issued token and use it to secure a request to
                // the web service.
                //
                Console.WriteLine("Calling the service with an issued token explicitly acquired using the WSTrustChannel...");
                CallServiceWithExplicitToken(GetIssuedToken());

                serviceHost.Close();
                serviceHost = null;

                securityTokenServiceHost.Close();
                securityTokenServiceHost = null;
            }
            catch (Exception ex)
            {
                Console.WriteLine("=== Unexpected exception caught ===");
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                if (serviceHost != null)
                {
                    serviceHost.Abort();
                }
                if (securityTokenServiceHost != null)
                {
                    securityTokenServiceHost.Abort();
                }
            }

            Console.WriteLine("Press <ENTER> to continue.");
            Console.ReadLine();
        }