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();
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // Create and setup the configuration for our STS
            SecurityTokenServiceConfiguration config = new SecurityTokenServiceConfiguration("STS");

            // Add the STS endpoint information
            config.TrustEndpoints.Add(
                new ServiceHostEndpointConfiguration(typeof(IWSTrust13SyncContract), GetSTSBinding(), "http://localhost:6000/STS"));

            // Set the STS implementation class type
            config.SecurityTokenService = typeof(CustomSecurityTokenService);
            SecurityTokenHandlerCollection actAsHandlerCollection = config.SecurityTokenHandlerCollectionManager[SecurityTokenHandlerCollectionManager.Usage.ActAs];

            actAsHandlerCollection.Configuration.IssuerNameRegistry = new ActAsIssuerNameRegistry();
            // The token that we receive in the <RequestSecurityToken><ActAs> element was issued to the service proxies.
            // By adding the proxy audience URIs here we are enforcing the implicit contract that the STS will accept
            // only tokens issued to the proxy as an ActAs token.
            actAsHandlerCollection.Configuration.AudienceRestriction.AllowedAudienceUris.Add(new Uri("https://localhost/WFE/default.aspx"));
            actAsHandlerCollection.Configuration.AudienceRestriction.AllowedAudienceUris.Add(new Uri("http://localhost/Service1/Service1.svc"));

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

                Console.WriteLine("STS started, press ENTER to stop ...");
                Console.ReadLine();

                host.Close();
            }
        }
        static void Main(string[] args)
        {
            WSTrustServiceHost stsHost = null;

            try
            {
                SecurityTokenServiceConfiguration stsConfiguration = new SecurityTokenServiceConfiguration(Address.StsAddress,
                                                                                                           new X509SigningCredentials(
                                                                                                               CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine,
                                                                                                                                              "CN=localhost")));
                stsConfiguration.SecurityTokenService = typeof(CustomSecurityTokenService);

                // Add the STS endpoint information
                stsConfiguration.TrustEndpoints.Add(
                    new ServiceHostEndpointConfiguration(typeof(IWSTrust13SyncContract), new WindowsWSTrustBinding(), Address.StsAddress));

                stsHost = new WSTrustServiceHost(stsConfiguration, new Uri(Address.StsAddress));
                stsHost.Open();

                Console.WriteLine("The security token service has started at {0}.\n", Address.StsAddress);
                Console.WriteLine("Press [Enter] to stop.\n");
                Console.ReadLine();
            }
            finally
            {
                try
                {
                    if (stsHost != null)
                    {
                        stsHost.Close();
                    }
                }
                catch (CommunicationException)
                {
                }
                catch (TimeoutException)
                {
                }
            }
        }
        static void Main(string[] args)
        {
            // Create and setup the configuration for our STS
            SigningCredentials signingCreds          = new X509SigningCredentials(CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, SigningCertificateName));
            SecurityTokenServiceConfiguration config = new SecurityTokenServiceConfiguration("http://SecurityTokenService", signingCreds);

            // Add the STS endoint information
            config.TrustEndpoints.Add(
                new ServiceHostEndpointConfiguration(typeof(IWSTrust13SyncContract), new WindowsWSTrustBinding(), "http://localhost:6000/SecurityTokenService"));

            // Set the STS implementation class type
            config.SecurityTokenService = typeof(MySecurityTokenService);

            // Create the WS-Trust service host with our STS configuration
            using (WSTrustServiceHost host = new WSTrustServiceHost(config, new Uri("http://localhost:6000/SecurityTokenService")))
            {
                host.Open();
                Console.WriteLine("SecurityTokenService started, press ENTER to stop ...");
                Console.ReadLine();
                host.Close();
            }
        }
        static void Main()
        {
            ServiceHost            serviceHost              = null;
            ChannelFactory <IEcho> echoChannelFactory       = null;
            WSTrustServiceHost     securityTokenServiceHost = null;

            try
            {
                //
                // Start the service
                //
                serviceHost = new ServiceHost(typeof(EchoService));
                string serviceAddress = "http://localhost:8080/EchoService";

                serviceHost.AddServiceEndpoint(typeof(IEcho), GetServiceBinding(), serviceAddress);
                ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
                metadataBehavior.HttpGetEnabled = true;
                metadataBehavior.HttpGetUrl     = new Uri(serviceAddress);
                serviceHost.Description.Behaviors.Add(metadataBehavior);
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), serviceAddress + "/mex");
                serviceHost.Credentials.ServiceCertificate.SetCertificate("CN=localhost", StoreLocation.LocalMachine, StoreName.My);

                serviceHost.Open();
                Console.WriteLine("The echo service has started at {0}.\n", serviceAddress);

                //
                // Start the STS
                //
                SecurityTokenServiceConfiguration securityTokenServiceConfiguration = new SecurityTokenServiceConfiguration(securityTokenServiceAddress, new X509SigningCredentials(serviceHost.Credentials.ServiceCertificate.Certificate));
                securityTokenServiceConfiguration.WSTrust13RequestSerializer = new CustomWSTrust13RequestSerializer();
                securityTokenServiceConfiguration.SecurityTokenService       = typeof(CustomSecurityTokenService);

                // Add the STS endpoint information
                securityTokenServiceConfiguration.TrustEndpoints.Add(
                    new ServiceHostEndpointConfiguration(typeof(IWSTrust13SyncContract), GetSecurityTokenServiceBinding(), securityTokenServiceAddress));

                securityTokenServiceHost = new WSTrustServiceHost(securityTokenServiceConfiguration, new Uri(securityTokenServiceAddress));
                securityTokenServiceHost.Open();

                Console.WriteLine("The security token service has started at {0}.\n", securityTokenServiceAddress);

                //
                // Invoke the client
                //
                echoChannelFactory = new ChannelFactory <IEcho>(GetClientBinding(), new EndpointAddress(new Uri(serviceAddress), EndpointIdentity.CreateDnsIdentity("localhost")));

                IEcho client = echoChannelFactory.CreateChannel();
                ((IClientChannel)client).OperationTimeout = TimeSpan.MaxValue;

                Console.WriteLine("The client sent a request to the STS to retrieve a SAML token and then sent the hello request to the echo service.\n");
                Console.WriteLine("The echo service finally returned '{0}'.\n", client.Echo("Hello"));

                Console.WriteLine("Press [Enter] to continue.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                try
                {
                    if (echoChannelFactory != null)
                    {
                        echoChannelFactory.Close();
                    }

                    if (serviceHost != null)
                    {
                        serviceHost.Close();
                    }

                    if (securityTokenServiceHost != null)
                    {
                        securityTokenServiceHost.Close();
                    }
                }
                catch (CommunicationException)
                {
                }
            }
        }
        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();
        }
Esempio n. 7
0
        static void Main()
        {
            ServiceHost            serviceHost        = null;
            ChannelFactory <IEcho> echoChannelFactory = null;
            WSTrustServiceHost     trustServiceHost   = null;

            try
            {
                CustomTokenHandler handler = new CustomTokenHandler();

                //
                // Start the service
                //
                serviceHost = new ServiceHost(typeof(EchoService));
                string serviceAddress = "http://" + Environment.MachineName + ":8080/EchoService";

                ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
                metadataBehavior.HttpGetEnabled = true;
                metadataBehavior.HttpGetUrl     = new Uri(serviceAddress);
                serviceHost.Description.Behaviors.Add(metadataBehavior);
                serviceHost.AddServiceEndpoint(typeof(IEcho), GetServiceBinding(), serviceAddress);
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), serviceAddress + "/mex");
                serviceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindByThumbprint, thumbprint);

                FederatedServiceCredentials.ConfigureServiceHost(serviceHost);
                //
                // Update the service credentials so that it can deserialize the custom token
                //
                (( FederatedServiceCredentials )serviceHost.Credentials).SecurityTokenHandlers.Add(handler);
                serviceHost.Open();
                Console.WriteLine("The echo service has started at {0}.\n", serviceAddress);

                //
                // Start the SecurityTokenService
                //
                X509Certificate2   certificate = CertificateUtil.GetCertificate(StoreName.Root, StoreLocation.LocalMachine, thumbprint);
                SigningCredentials credentials = new X509SigningCredentials(certificate);
                SecurityTokenServiceConfiguration securityTokenServiceConfiguration = new SecurityTokenServiceConfiguration(securityTokenServiceAddress, credentials);
                securityTokenServiceConfiguration.SecurityTokenService = typeof(SampleTokenService);

                // register a handler to the SecurityTokenService here so that it can issue the custom token
                securityTokenServiceConfiguration.SecurityTokenHandlers.Add(handler);

                // Add the STS endpoint information
                securityTokenServiceConfiguration.TrustEndpoints.Add(
                    new ServiceHostEndpointConfiguration(typeof(IWSTrust13SyncContract), GetSecurityTokenServiceBinding(), securityTokenServiceAddress));

                securityTokenServiceConfiguration.ServiceCertificate = certificate;

                trustServiceHost = new WSTrustServiceHost(securityTokenServiceConfiguration, new Uri(securityTokenServiceAddress));
                trustServiceHost.Open();
                Console.WriteLine("The security token service has started at {0}.\n", securityTokenServiceAddress);

                //
                // Invoke the client
                //



                echoChannelFactory = new ChannelFactory <IEcho>(GetClientBinding(),
                                                                new EndpointAddress(new Uri(serviceAddress),
                                                                                    EndpointIdentity.CreateDnsIdentity("localhost")));

                IEcho client = echoChannelFactory.CreateChannel();
                ((IClientChannel)client).OperationTimeout = TimeSpan.MaxValue;


                string echoedString = client.Echo("Hello");
                Console.WriteLine("The echo service returns '{0}'. \n", echoedString);


                Console.WriteLine("Press [Enter] to close service.");
                Console.ReadLine();

                echoChannelFactory.Close();

                Console.WriteLine("Press [Enter] to continue.");
                Console.ReadLine();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
                if (echoChannelFactory != null)
                {
                    echoChannelFactory.Abort();
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);
                if (echoChannelFactory != null)
                {
                    echoChannelFactory.Abort();
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.InnerException.Message);
            }
            finally
            {
                if (serviceHost != null && serviceHost.State != CommunicationState.Faulted)
                {
                    serviceHost.Close();
                }

                if (trustServiceHost != null && trustServiceHost.State != CommunicationState.Faulted)
                {
                    trustServiceHost.Close();
                }
            }
        }