コード例 #1
0
        public TenantCredential(AadEnvironment environment, string tenant, string clientId, ApplicationType applicationType, string alias)
        {
            if (environment == null)
            {
                throw new ArgumentException(StringResources.EnvironmentCannotBeNull);
            }

            if (String.IsNullOrEmpty(tenant))
            {
                throw new ArgumentException(StringResources.TenantCannotBeNullOrWhiteSpace);
            }

            if (String.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentException(StringResources.ClientIdCannotBeNullOrWhiteSpace);
            }

            Guid ignored;

            if (!Guid.TryParse(clientId, out ignored))
            {
                throw new ArgumentException(StringResources.ClientIdMustBeAGuid);
            }

            this.Environment     = environment;
            this.Tenant          = tenant;
            this.ClientId        = clientId;
            this.ApplicationType = applicationType;
            this.Alias           = alias;
        }
コード例 #2
0
        public void Store(AadEnvironment environment)
        {
            string environmentPath = GetEnvironmentPath(environment);

            using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(environmentPath))
            {
                if (registryKey == null)
                {
                    return;
                }

                registryKey.SetValue(LoginEndpointKey, environment.LoginEndpoint);
                registryKey.SetValue(GraphApiEndpointKey, environment.GraphApiEndpoint);
            }
        }
コード例 #3
0
        private void PopulateTenantCredentials(AadEnvironment environment, string lastSelectedTenantCredential = null)
        {
            if (environment == null)
            {
                return;
            }

            this.TenantCredentialComboBox.Items.Clear();
            foreach (TenantCredential tenantCredential in this.Store.GetTenantCredentials(environment))
            {
                this.TenantCredentialComboBox.Items.Add(tenantCredential);
                if (tenantCredential.ToString().Equals(lastSelectedTenantCredential, StringComparison.OrdinalIgnoreCase))
                {
                    this.TenantCredentialComboBox.SelectedItem = tenantCredential;
                }
            }

            this.TenantCredentialComboBox.Items.Add(StringResources.ManageItem);
            this.getAppTokenButton.Enabled = this.TenantCredentialComboBox.SelectedItem != null;
        }
コード例 #4
0
        private void environmentComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.EnvironmentComboBox.SelectedItem == null)
            {
                return;
            }

            AadEnvironment environment = this.EnvironmentComboBox.SelectedItem as AadEnvironment;

            this.PopulateTenantCredentials(environment);
            this.urlBuilder.Environment = environment;

            this.apiVersionComboBox.Items.Clear();
            if (environment != null)
            {
                foreach (string apiVersion in environment.ApiVersions)
                {
                    this.apiVersionComboBox.Items.Add(apiVersion);
                }
            }
        }
コード例 #5
0
 public void Delete(AadEnvironment environment)
 {
 }
コード例 #6
0
        private static string GetTenantPath(AadEnvironment environment, string tenant)
        {
            string environmentPath = GetEnvironmentPath(environment);

            return(environmentPath + '\\' + tenant);
        }
コード例 #7
0
 private static string GetEnvironmentPath(AadEnvironment environment)
 {
     return(EnvironmentsRoot + '\\' + environment.RegistryName);
 }
コード例 #8
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);
        }