Exemplo n.º 1
0
        private void TenantCredentialComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.TenantCredentialComboBox.SelectedItem == null)
            {
                this.getAppTokenButton.Enabled = false;
                return;
            }

            if (this.TenantCredentialComboBox.SelectedItem.Equals(StringResources.ManageItem))
            {
                this.getAppTokenButton.Enabled = false;
                new AddTenantCredentialForm(this).ShowDialog();
            }

            TenantCredential tenantCredential = this.TenantCredentialComboBox.SelectedItem as TenantCredential;

            if (tenantCredential == null)
            {
                this.getAppTokenButton.Enabled = false;
                return;
            }

            this.getAppTokenButton.Text = tenantCredential.ApplicationType == ApplicationType.Web
                                              ? StringResources.GetAppTokenText
                                              : StringResources.GetUserTokenText;
            this.getAppTokenButton.Enabled   = true;
            this.urlBuilder.TenantCredential = tenantCredential;

            this.UpdateRequestUrl();
        }
Exemplo n.º 2
0
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TenantCredential tenantCredential = this.TenantCredentialComboBox.SelectedItem as TenantCredential;

            if (tenantCredential == null)
            {
                return;
            }

            string messageFormat = "Deleting Following Tenant: \n Env: {0}\n Tenant: {1}\n Client ID: {2}: \n Application type:{3}";
            string message       = String.Format(messageFormat, tenantCredential.Environment, tenantCredential.Tenant, tenantCredential.ClientId, tenantCredential.ApplicationType);

            DialogResult dr = MessageBox.Show(message, "Confirm Delete", MessageBoxButtons.OKCancel);

            if (dr == DialogResult.OK)
            {
                bool res = Store.Delete(tenantCredential);

                MessageBox.Show("Delete was " + ((res) ? "successful" : "Unsuccessful"));

                if (res)
                {
                    TenantCredentialComboBox.Items.Remove(tenantCredential);
                }
            }
        }
Exemplo n.º 3
0
        private void getAppTokenButton_Click(object sender, EventArgs e)
        {
            TenantCredential tenantCredential = this.TenantCredentialComboBox.SelectedItem as TenantCredential;

            if (tenantCredential == null)
            {
                return;
            }

            Func <TenantCredential, string> getTokenFunc = Token.GetApplicationToken;

            if (tenantCredential.ApplicationType == ApplicationType.Native)
            {
                getTokenFunc = Token.GetUserToken;
            }

            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.tokenTextBox.Text           = getTokenFunc(tenantCredential);
                this.Cursor                      = Cursors.Default;
                this.urlBuilder.Environment      = this.EnvironmentComboBox.SelectedItem as AadEnvironment;
                this.urlBuilder.TenantCredential = this.TenantCredentialComboBox.SelectedItem as TenantCredential;
                //this.UpdateRequestUrl();
            }
            catch (Exception exception)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(exception.Message, StringResources.TokenErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void addButton_Click(object sender, EventArgs e)
        {
            ApplicationType applicationType = ApplicationType.Web;

            if (this.nativeAppRadioButton.Checked)
            {
                applicationType = ApplicationType.Native;
            }

            TenantCredential tenantCredential = new TenantCredential(
                (AadEnvironment)this.mainForm.EnvironmentComboBox.SelectedItem,
                this.tenantTextBox.Text.Trim().ToLowerInvariant(),
                this.clientIdTextBox.Text.Trim().ToLowerInvariant(),
                applicationType,
                aliasTextBox.Text);

            if (applicationType == ApplicationType.Native)
            {
                tenantCredential.ReplyUrl = new Uri(this.keyOrReplyUrlTextBox.Text);
            }
            else
            {
                tenantCredential.EncryptAndSetKey(this.keyOrReplyUrlTextBox.Text.Trim());
            }

            this.mainForm.TenantCredentialComboBox.Items.Add(tenantCredential);
            this.mainForm.TenantCredentialComboBox.SelectedItem = tenantCredential;

            if (this.nativeAppRadioButton.Checked || (this.webAppRadioButton.Checked && this.saveCredentialCheckBox.Checked))
            {
                this.mainForm.Store.Store(tenantCredential);
            }

            this.Close();
        }
Exemplo n.º 5
0
        internal static string GetApplicationToken(TenantCredential tenantCredential)
        {
            string loginUrl          = tenantCredential.Environment.GetLoginUrl(tenantCredential.Tenant);
            AuthenticationContext ac = new AuthenticationContext(loginUrl, false);
            ClientCredential      clientCredential     = new ClientCredential(tenantCredential.ClientId, tenantCredential.GetDecryptedKey());
            AuthenticationResult  authenticationResult = ac.AcquireToken(tenantCredential.Environment.GraphResourceUrl, clientCredential);

            return(authenticationResult.CreateAuthorizationHeader());
        }
Exemplo n.º 6
0
        internal static string GetUserToken(TenantCredential tenantCredential)
        {
            string loginUrl          = tenantCredential.Environment.GetLoginUrl(tenantCredential.Tenant);
            AuthenticationContext ac = new AuthenticationContext(loginUrl, false);

            var promptBehaviour = PromptBehavior.Auto;

            if (lastUserTenantCredential != null && !lastUserTenantCredential.Tenant.Equals(tenantCredential.Tenant))
            {
                promptBehaviour = PromptBehavior.Always;
            }

            lastUserTenantCredential = tenantCredential;

            AuthenticationResult authenticationResult = ac.AcquireToken(
                tenantCredential.Environment.GraphResourceUrl,
                tenantCredential.ClientId,
                tenantCredential.ReplyUrl,
                promptBehaviour);

            return(authenticationResult.CreateAuthorizationHeader());
        }
Exemplo n.º 7
0
        public void Store(TenantCredential tenantCredential)
        {
            string credentialPath = GetCredentialPath(tenantCredential);

            using (RegistryKey credentialKey = Registry.CurrentUser.CreateSubKey(credentialPath))
            {
                if (credentialKey == null)
                {
                    return;
                }

                credentialKey.SetValue(ApplicationTypeKey, tenantCredential.ApplicationType.ToString());
                credentialKey.SetValue(AliasKey, tenantCredential.Alias);

                if (tenantCredential.ApplicationType == ApplicationType.Native)
                {
                    credentialKey.SetValue(ReplyUrlKey, tenantCredential.ReplyUrl);
                }
                else
                {
                    credentialKey.SetValue(EncryptedKeyKey, tenantCredential.EncryptedKey);
                }
            }
        }
Exemplo n.º 8
0
        private static string GetCredentialPath(TenantCredential tenantCredential)
        {
            string tenantPath = GetTenantPath(tenantCredential.Environment, tenantCredential.Tenant);

            return(tenantPath + '\\' + tenantCredential.ClientId);
        }
Exemplo n.º 9
0
        public TenantCredentialSet GetTenantCredentials(AadEnvironment environment)
        {
            TenantCredentialSet tenantCredentials = new TenantCredentialSet();
            string environmentPath = GetEnvironmentPath(environment);

            using (RegistryKey environmentKey = Registry.CurrentUser.OpenSubKey(environmentPath))
            {
                if (environmentKey == null)
                {
                    return(tenantCredentials);
                }

                foreach (string tenant in environmentKey.GetSubKeyNames())
                {
                    using (RegistryKey tenantKey = environmentKey.OpenSubKey(tenant))
                    {
                        if (tenantKey == null)
                        {
                            continue;
                        }

                        foreach (string clientId in tenantKey.GetSubKeyNames())
                        {
                            using (RegistryKey clientIdKey = tenantKey.OpenSubKey(clientId))
                            {
                                if (String.IsNullOrWhiteSpace(clientId))
                                {
                                    continue;
                                }

                                if (clientIdKey == null)
                                {
                                    continue;
                                }

                                string          applicationTypeString = clientIdKey.GetValue(ApplicationTypeKey) as string;
                                ApplicationType applicationType;
                                if (!Enum.TryParse(applicationTypeString, true, out applicationType))
                                {
                                    continue;
                                }

                                string alias = clientIdKey.GetValue(AliasKey) as string;

                                TenantCredential credential;
                                try
                                {
                                    credential = new TenantCredential(environment, tenant, clientId, applicationType, alias);
                                }
                                catch (Exception)
                                {
                                    // Bad key, can't decrypt :-(
                                    continue;
                                }

                                switch (applicationType)
                                {
                                case ApplicationType.Native:
                                    string replyUrl = clientIdKey.GetValue(ReplyUrlKey) as string;
                                    if (string.IsNullOrWhiteSpace(replyUrl))
                                    {
                                        continue;
                                    }

                                    credential.ReplyUrl = new Uri(replyUrl);
                                    break;

                                case ApplicationType.Web:
                                    byte[] encryptedKey = clientIdKey.GetValue(EncryptedKeyKey) as byte[];
                                    if (encryptedKey == null || encryptedKey.Length < 16)
                                    {
                                        continue;
                                    }

                                    credential.EncryptedKey = encryptedKey;
                                    break;

                                default:
                                    // unknown application type :-(
                                    continue;
                                }

                                tenantCredentials.Add(credential);
                            }
                        }
                    }
                }
            }

            return(tenantCredentials);
        }