/// <summary>
        /// Initializes the user identity control.
        /// </summary>
        private void InitializeUserIdentity(ConfiguredEndpoint endpoint)
        {
            // get the current user token type.
            UserTokenItem currentItem = new UserTokenItem(UserTokenType.Anonymous);

            if (UserTokenTypeCB.SelectedIndex != -1)
            {
                currentItem = (UserTokenItem)UserTokenTypeCB.SelectedItem;
            }

            // get the identity.
            UserIdentityToken identity = null;
            m_userIdentities.TryGetValue(currentItem.ToString(), out identity);

            // set the default values.
            UserIdentityTB.Text = "";
            UserIdentityTB.IsEnabled = currentItem.Policy.TokenType != UserTokenType.Anonymous;
            UserIdentityBTN.IsEnabled = currentItem.Policy.TokenType != UserTokenType.Anonymous;

            // update from endpoint.
            if (identity != null)
            {
                UserNameIdentityToken userNameToken = identity as UserNameIdentityToken;

                if (userNameToken != null)
                {
                    UserIdentityTB.Text = userNameToken.UserName;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Displays the dialog.
        /// </summary>
        public ConfiguredEndpoint ShowDialog(ConfiguredEndpoint endpoint, ApplicationConfiguration configuration)
        {
            if (endpoint == null) throw new ArgumentNullException("endpoint");

            m_endpoint = endpoint;
            m_configuration = configuration;

            // construct a list of available endpoint descriptions for the application.
            m_availableEndpoints = new EndpointDescriptionCollection();
            m_availableEndpointsDescriptions = new List<EndpointDescriptionString>();

            m_availableEndpoints.Add(endpoint.Description);
            m_currentDescription = endpoint.Description;
            m_endpointConfiguration = endpoint.Configuration;

            if (m_endpointConfiguration == null)
            {
                m_endpointConfiguration = EndpointConfiguration.Create(configuration);
            }

            if (endpoint.Collection != null)
            {
                foreach (ConfiguredEndpoint existingEndpoint in endpoint.Collection.Endpoints)
                {
                    if (existingEndpoint.Description.Server.ApplicationUri == endpoint.Description.Server.ApplicationUri)
                    {
                        m_availableEndpoints.Add(existingEndpoint.Description);
                    }
                }
            }

            BuildEndpointDescriptionStrings(m_availableEndpoints);

            UserTokenPolicy policy = m_endpoint.SelectedUserTokenPolicy;

            if (policy == null)
            {
                if (m_endpoint.Description.UserIdentityTokens.Count > 0)
                {
                    policy = m_endpoint.Description.UserIdentityTokens[0];
                }
            }

            if (policy != null)
            {
                UserTokenItem userTokenItem = new UserTokenItem(policy);

                if (policy.TokenType == UserTokenType.UserName && m_endpoint.UserIdentity is UserNameIdentityToken)
                {
                    m_userIdentities[userTokenItem.ToString()] = m_endpoint.UserIdentity;
                }

                if (policy.TokenType == UserTokenType.Certificate && m_endpoint.UserIdentity is X509IdentityToken)
                {
                    m_userIdentities[userTokenItem.ToString()] = m_endpoint.UserIdentity;
                }

                if (policy.TokenType == UserTokenType.IssuedToken && m_endpoint.UserIdentity is IssuedIdentityToken)
                {
                    m_userIdentities[userTokenItem.ToString()] = m_endpoint.UserIdentity;
                }
            }

            // copy com identity.
            m_comIdentity = endpoint.ComIdentity;

            // initializing the protocol will trigger an update to all other controls.
            InitializeProtocols(m_availableEndpoints);

            // check if the current settings match the defaults.
            EndpointConfiguration defaultConfiguration = EndpointConfiguration.Create(configuration);

            // discover endpoints in the background.
            Interlocked.Increment(ref m_discoverCount);
            ThreadPool.QueueUserWorkItem(new WaitCallback(OnDiscoverEndpoints), m_endpoint.Description.Server);

            if (ShowDialog() != DialogResult.OK)
            {
                return null;
            }

            return m_endpoint;
        }
        /// <summary>
        /// Initializes the user identity control.
        /// </summary>
        private void InitializeIssuedTokenType(EndpointDescription endpoint)
        {
            // get the current user token type.
            UserTokenItem currentTokenType = new UserTokenItem(UserTokenType.Anonymous);

            if (UserTokenTypeCB.SelectedIndex != -1)
            {
                currentTokenType = (UserTokenItem)UserTokenTypeCB.SelectedItem;
            }

            // preserve the existing value.
            string currentIssuedTokenType = (string)IssuedTokenTypeCB.SelectedItem;

            IssuedTokenTypeCB.Items.Clear();
            IssuedTokenTypeCB.IsEnabled = false;

            // only applies to issued tokens.
            if (currentTokenType.Policy.TokenType != UserTokenType.IssuedToken)
            {
                return;
            }

            // only one item to select.
            if (currentTokenType.Policy.IssuedTokenType != null)
            {
                IssuedTokenTypeCB.Items.Add(currentTokenType.Policy.IssuedTokenType);
                IssuedTokenTypeCB.SelectedIndex = 0;
                IssuedTokenTypeCB.IsEnabled = true;
            }
        }
        /// <summary>
        /// Initializes the user token types dropdown.
        /// </summary>
        private void InitializeUserTokenTypes(EndpointDescription endpoint)
        {
            // preserve the existing value.
            UserTokenItem currentTokenType = new UserTokenItem(UserTokenType.Anonymous);

            if (UserTokenTypeCB.SelectedIndex != -1)
            {
                currentTokenType = (UserTokenItem)UserTokenTypeCB.SelectedItem;
            }

            UserTokenTypeCB.Items.Clear();

            // show all options.
            if (m_showAllOptions)
            {
                UserTokenTypeCB.Items.Add(new UserTokenItem(UserTokenType.Anonymous));
                UserTokenTypeCB.Items.Add(new UserTokenItem(UserTokenType.UserName));
                UserTokenTypeCB.Items.Add(new UserTokenItem(UserTokenType.Certificate));
                UserTokenTypeCB.Items.Add(new UserTokenItem(UserTokenType.IssuedToken));
            }

            // find all unique token types.  
            else
            {
                if (endpoint != null)
                {
                    foreach (UserTokenPolicy policy in endpoint.UserIdentityTokens)
                    {
                        UserTokenTypeCB.Items.Add(new UserTokenItem(policy));
                    }
                }

                // add at least one policy.
                if (UserTokenTypeCB.Items.Count == 0)
                {
                    UserTokenTypeCB.Items.Add(new UserTokenItem(UserTokenType.Anonymous));
                }
            }

            int index = -1;

            // try to match policy id.
            for (int ii = 0; ii < UserTokenTypeCB.Items.Count; ii++)
            {
                UserTokenItem item = (UserTokenItem)UserTokenTypeCB.Items[ii];

                if (item.Policy.PolicyId == currentTokenType.Policy.PolicyId)
                {
                    index = ii;
                    break;
                }
            }

            // match user token type.
            if (index == -1)
            {
                index = 0;

                for (int ii = 0; ii < UserTokenTypeCB.Items.Count; ii++)
                {
                    UserTokenItem item = (UserTokenItem)UserTokenTypeCB.Items[ii];

                    if (item.Policy.TokenType == currentTokenType.Policy.TokenType)
                    {
                        index = ii;
                        break;
                    }
                }
            }

            UserTokenTypeCB.SelectedIndex = index;
        }
        /// <summary>
        /// Finds the best match for the current protocol and security selections.
        /// </summary>
        private int FindBestUserTokenPolicy(EndpointDescription endpoint)
        {
            // filter by the current token type.
            UserTokenItem currentTokenType = new UserTokenItem(UserTokenType.Anonymous);

            if (UserTokenTypeCB.SelectedIndex != -1)
            {
                currentTokenType = (UserTokenItem)UserTokenTypeCB.SelectedItem;
            }

            // filter by issued token type.
            string currentIssuedTokenType = (string)IssuedTokenTypeCB.SelectedItem;

            // find all matching descriptions.      
            UserTokenPolicyCollection matches = new UserTokenPolicyCollection();

            if (endpoint != null)
            {
                for (int ii = 0; ii < endpoint.UserIdentityTokens.Count; ii++)
                {
                    UserTokenPolicy policy = endpoint.UserIdentityTokens[ii];

                    if (currentTokenType.Policy.PolicyId == policy.PolicyId)
                    {
                        return ii;
                    }
                }

                for (int ii = 0; ii < endpoint.UserIdentityTokens.Count; ii++)
                {
                    UserTokenPolicy policy = endpoint.UserIdentityTokens[ii];

                    if (currentTokenType.Policy.TokenType != policy.TokenType)
                    {
                        continue;
                    }

                    if (policy.TokenType == UserTokenType.IssuedToken)
                    {
                        if (currentIssuedTokenType != policy.IssuedTokenType)
                        {
                            continue;
                        }
                    }

                    return ii;
                }
            }

            return -1;
        }
        /// <summary>
        /// Displays the dialog.
        /// </summary>
        public async Task<ConfiguredEndpoint> ShowDialog(ConfiguredEndpoint endpoint, ApplicationConfiguration configuration)
        {
            if (endpoint == null) throw new ArgumentNullException("endpoint");

            m_endpoint = endpoint;
            m_configuration = configuration;

            // construct a list of available endpoint descriptions for the application.
            m_availableEndpoints = new EndpointDescriptionCollection();

            m_availableEndpoints.Add(endpoint.Description);
            m_currentDescription = endpoint.Description;
            m_endpointConfiguration = endpoint.Configuration;

            if (m_endpointConfiguration == null)
            {
                m_endpointConfiguration = EndpointConfiguration.Create(configuration);
            }

            if (endpoint.Collection != null)
            {
                foreach (ConfiguredEndpoint existingEndpoint in endpoint.Collection.Endpoints)
                {
                    if (existingEndpoint.Description.Server.ApplicationUri == endpoint.Description.Server.ApplicationUri)
                    {
                        m_availableEndpoints.Add(existingEndpoint.Description);
                    }
                }
            }

            UserTokenPolicy policy = m_endpoint.SelectedUserTokenPolicy;

            if (policy == null)
            {
                if (m_endpoint.Description.UserIdentityTokens.Count > 0)
                {
                    policy = m_endpoint.Description.UserIdentityTokens[0];
                }
            }

            if (policy != null)
            {
                UserTokenItem userTokenItem = new UserTokenItem(policy);

                if (policy.TokenType == UserTokenType.UserName && m_endpoint.UserIdentity is UserNameIdentityToken)
                {
                    m_userIdentities[userTokenItem.ToString()] = m_endpoint.UserIdentity;
                }

                if (policy.TokenType == UserTokenType.Certificate && m_endpoint.UserIdentity is X509IdentityToken)
                {
                    m_userIdentities[userTokenItem.ToString()] = m_endpoint.UserIdentity;
                }

                if (policy.TokenType == UserTokenType.IssuedToken && m_endpoint.UserIdentity is IssuedIdentityToken)
                {
                    m_userIdentities[userTokenItem.ToString()] = m_endpoint.UserIdentity;
                }

                UserTokenTypeCB.Items.Add(userTokenItem);
                UserTokenTypeCB.SelectedIndex = UserTokenTypeCB.Items.IndexOf(userTokenItem);
            }

            // copy com identity.
            m_comIdentity = endpoint.ComIdentity;

            // initializing the protocol will trigger an update to all other controls.
            InitializeProtocols(m_availableEndpoints);

            // check if the current settings match the defaults.
            EndpointConfiguration defaultConfiguration = EndpointConfiguration.Create(configuration);

            if (SameAsDefaults(defaultConfiguration, m_endpoint.Configuration))
            {
                UseDefaultLimitsCB.SelectedIndex = (int)UseDefaultLimits.Yes;
            }
            else
            {
                UseDefaultLimitsCB.SelectedIndex = (int)UseDefaultLimits.No;
            }

            // discover endpoints in the background.
            Interlocked.Increment(ref m_discoverCount);
            OnDiscoverEndpoints(m_endpoint.Description.Server);

            TaskCompletionSource<ConfiguredEndpoint> tcs = new TaskCompletionSource<ConfiguredEndpoint>();
            // display dialog
            dialogPopup.Child = this;
            dialogPopup.IsOpen = true;
            dialogPopup.Closed += (o, e) =>
            {
                tcs.SetResult(m_endpoint);
            };
            return await tcs.Task;
        }
        private async void UserIdentityBTN_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                UserTokenItem currentItem = new UserTokenItem(UserTokenType.Anonymous);

                if (UserTokenTypeCB.SelectedIndex != -1)
                {
                    currentItem = (UserTokenItem)UserTokenTypeCB.SelectedItem;
                }

                UserIdentityToken identity = null;
                m_userIdentities.TryGetValue(currentItem.ToString(), out identity);

                switch (currentItem.Policy.TokenType)
                {
                    case UserTokenType.UserName:
                        {
                            UserNameIdentityToken userNameToken = identity as UserNameIdentityToken;

                            if (userNameToken == null)
                            {
                                userNameToken = new UserNameIdentityToken();
                            }

                            if (new UsernameTokenDlg().ShowDialog(userNameToken))
                            {
                                userNameToken.PolicyId = currentItem.Policy.PolicyId;
                                m_userIdentities[currentItem.ToString()] = userNameToken;
                                UserIdentityTB.Text = userNameToken.UserName;
                            }

                            break;
                        }

                    default:
                        {
                            MessageDlg dialog = new MessageDlg("User token type not supported at this time.");
                            await dialog.ShowAsync();
                            break;
                        }
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(String.Empty, GuiUtils.CallerName(), exception);
            }
        }