protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                return;
            }

            string value = Request["value"];

            if (String.IsNullOrEmpty(value))
            {
                value = "Default Input";
            }

            // Get the Bootstrap Token
            SecurityToken callerToken = null;

            IClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as IClaimsPrincipal;

            // We expect only one identity, which will contain the bootstrap token.
            if (claimsPrincipal != null && claimsPrincipal.Identities.Count == 1)
            {
                callerToken = claimsPrincipal.Identities[0].BootstrapToken;
            }

            if (callerToken == null)
            {
                // We lost the session state but the user still has the federated ticket
                // Let's sign the user off and start again
                FederatedAuthentication.SessionAuthenticationModule.DeleteSessionTokenCookie();
                Response.Redirect(Request.Url.AbsoluteUri);
                return;
            }

            // Get the channel factory to the backend service from the application state
            ChannelFactory <IService2Channel> factory = (ChannelFactory <IService2Channel>)Application[Global.CachedChannelFactory];

            // Create and setup channel to talk to the backend service
            IService2Channel channel;

            // Setup the ActAs to point to the caller's token so that we perform a delegated call to the backend service
            // on behalf of the original caller.
            //
            // Note: A new channel must be created for each call.
            channel = factory.CreateChannelActingAs <IService2Channel>(callerToken);

            string retval = null;

            // Call the backend service and handle the possible exceptions
            try
            {
                retval = channel.ComputeResponse(value);
                channel.Close();
            }
            catch (CommunicationException exception)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(exception.Message);
                sb.AppendLine(exception.StackTrace);
                Exception ex = exception.InnerException;
                while (ex != null)
                {
                    sb.AppendLine("===========================");
                    sb.AppendLine(ex.Message);
                    sb.AppendLine(ex.StackTrace);
                    ex = ex.InnerException;
                }
                channel.Abort();
                retval = sb.ToString();;
            }
            catch (TimeoutException)
            {
                channel.Abort();
                retval = "Timed out...";
            }
            catch (Exception exception)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("An unexpected exception occured.");
                sb.AppendLine(exception.StackTrace);
                channel.Abort();
                retval = sb.ToString();
            }

            // Make the result visible on the page
            lblResult.Text = Server.HtmlEncode(retval);
        }
Esempio n. 2
0
        /// <summary>
        /// Echo service that calls the backend service acting as the client. A SAML token is obtained from the STS
        /// Acting on behalf of the client and then the SAML token is presented to the backend service.
        /// </summary>
        /// <param name="value">The client supplied value in the call to this service.</param>
        /// <returns>A string that the client provided.</returns>
        public string Echo(string value)
        {
            IClaimsIdentity identity = (IClaimsIdentity)((IClaimsPrincipal)Thread.CurrentPrincipal).Identity;

            Console.WriteLine("Caller's name: " + identity.Name);
            foreach (Claim claim in identity.Claims)
            {
                Console.WriteLine("ClaimType  : " + claim.ClaimType);
                Console.WriteLine("ClaimValue : " + claim.Value);
                Console.WriteLine();
            }

            string retval = null;

            lock ( service2CFLock )
            {
                if (service2CF == null)
                {
                    // Create a ChannelFactory to talk to the Service.
                    service2CF = new ChannelFactory <IService2Channel>("CustomBinding_IService2");
                    service2CF.ConfigureChannelFactory();
                }
            }

            // Get the client token. Here we are looking for a SAML token as we know from our bindings that the
            // client should have authenticated using a SAML 1.1 token.
            SecurityToken clientToken = null;

            IClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as IClaimsPrincipal;

            if (claimsPrincipal != null)
            {
                foreach (IClaimsIdentity claimsIdentity in claimsPrincipal.Identities)
                {
                    if (claimsIdentity.BootstrapToken is SamlSecurityToken)
                    {
                        clientToken = claimsIdentity.BootstrapToken;
                        break;
                    }
                }
            }

            // We couldn't find the client token. This would mean that either IdentityModel's SecurityTokenHandler
            // was not involved in validating this token or IdentityModelServiceAuthorizationManager was not
            // plugged into the ServiceHost.
            if (clientToken == null)
            {
                throw new InvalidOperationException(
                          "Unable to find client token. Check if your ServiceHost is correctly configured.");
            }

            // Create a channel that uses the Client token as the ActAs token. The Channel will use
            // the configured client token when an ActAs token is requested.
            //
            // Note: A new channel must be created for each call.
            IService2Channel channel = service2CF.CreateChannelActingAs <IService2Channel>(clientToken);

            try
            {
                // Call the backend service.
                retval = channel.ComputeResponse(value);
                channel.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Exception ex = e.InnerException;
                while (ex != null)
                {
                    Console.WriteLine("===========================");
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                    ex = ex.InnerException;
                }
                channel.Abort();
                retval = "Unable to compute the return value";
            }
            catch (TimeoutException)
            {
                Console.WriteLine("Timed out...");
                channel.Abort();
                retval = "Unable to compute the return value";
            }
            catch (Exception e)
            {
                Console.WriteLine("An unexpected exception occured.");
                Console.WriteLine(e.StackTrace);
                channel.Abort();
                retval = "Unable to compute the return value";
            }

            return(retval);
        }