private void button_Next_Click()

in Office365APIEditor/UI/AccessTokenWizard.cs [205:675]


        private void button_Next_Click(object sender, EventArgs e)
        {
            ValidateResult validateResult;
            AcquireAccessTokenResult acquireAccessTokenResult;

            switch (currentPageIndex)
            {
                case PageIndex.Page00_PortalSelection:
                    // Portal selection page

                    if (radioButton_Page00_MicrosoftAzurePortal.Checked)
                    {
                        // V1 endpoint
                        // Go to the next page.
                        ShowPage(PageIndex.Page01_MicrosoftAzurePortalAppSelection);
                    }
                    else if (radioButton_Page00_AppRegistrationPortal.Checked)
                    {
                        // V2 endpoint
                        // Go to the next page.
                        ShowPage(PageIndex.Page02_AppRegistrationPortalAppSelection);
                    }
                    else if (radioButton_Page00_BuiltInAppOrBasicAuth.Checked)
                    {
                        // The user has no application
                        // Go to the next page.
                        // Create a return value and close this window.
                        ShowPage(PageIndex.Page10_BuiltInAppOrBasicAuthSelection);
                    }
                    else
                    {
                        // The user has the SharePoint Online App-Only REST API
                        // Microsoft Azure Access Control Service application.
                        ShowPage(PageIndex.Page14_SharePointOnlineAppOnlyOptionForm);
                    }

                    break;
                case PageIndex.Page01_MicrosoftAzurePortalAppSelection:
                    // App selection page for V1 auth endpoint.

                    if (radioButton_Page01_V1Web.Checked)
                    {
                        // V1 endpoint Web App
                        // Go to the next page.
                        ShowPage(PageIndex.Page03_V1WebAppOptionForm);
                    }
                    else if (radioButton_Page01_V1Native.Checked)
                    {
                        // V1 endpoint Native App
                        // Go to the next page.
                        ShowPage(PageIndex.Page04_V1NativeAppOptionForm);
                    }
                    else if (radioButton_Page01_V1AppOnlyByCert.Checked)
                    {
                        // V1 endpoint Web App (App Only Token by certificate)
                        // Go to the next page.
                        ShowPage(PageIndex.Page05_V1AppOnlyByCertOptionForm);
                    }
                    else if (radioButton_Page01_V1AppOnlyByKey.Checked)
                    {
                        // V1 endpoint Web App (App Only Token by Key)
                        // Go to the next page.
                        ShowPage(PageIndex.Page08_V1AppOnlyByKeyOptionForm);
                    }
                    else
                    {
                        // V1 endpoint Admin Consent
                        // Go to the next page.
                        ShowPage(PageIndex.Page09_V1AdminConsentOptionForm);
                    }

                    break;
                case PageIndex.Page02_AppRegistrationPortalAppSelection:
                    // App selection page for V2 auth endpoint.

                    if (radioButton_Page02_V2Web.Checked)
                    {
                        // V2 endpoint Web App
                        // Go to the next page.
                        ShowPage(PageIndex.Page06_V2WebAppOptionForm);
                    }
                    else if (radioButton_Page02_V2Mobile.Checked)
                    {
                        // V2 endpoint Mobile App
                        // Go to the next page.
                        ShowPage(PageIndex.Page07_V2MobileAppOptionForm);
                    }
                    else if (radioButton_Page02_V2AdminConsent.Checked)
                    {
                        // V2 endpoint, but need admin consent
                        // Go to the next page.
                        ShowPage(PageIndex.Page13_V2AdminConsentOptionForm);
                    }
                    else if (radioButton_Page02_V2WebAppOnlyForMicrosoftGraph.Checked)
                    {
                        // V2 endpoint Web App (App Only Token by password for Microsoft Graph)
                        // Go to the next page.
                        ShowPage(PageIndex.Page12_V2AppOnlyByPasswordForMicrosoftGraphOptionForm);
                    }

                    break;
                case PageIndex.Page03_V1WebAppOptionForm:
                    // Option form for V1 auth endpoint Web App

                    V1WebAppUtil v1WebAppUtil = new V1WebAppUtil()
                    {
                        ClientID = textBox_Page03_ClientID.Text,
                        RedirectUri = textBox_Page03_RedirectUri.Text,
                        Resource = Util.ConvertResourceNameToResourceEnum(comboBox_Page03_Resource.SelectedItem.ToString()),
                        ClientSecret = textBox_Page03_ClientSecret.Text
                    };

                    validateResult = v1WebAppUtil.Validate();

                    if (validateResult.IsValid)
                    {
                        acquireAccessTokenResult = v1WebAppUtil.AcquireAccessToken();

                        if (acquireAccessTokenResult.Success == InteractiveResult.Fail)
                        {
                            MessageBox.Show(acquireAccessTokenResult.ErrorMessage, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else if (acquireAccessTokenResult.Success == InteractiveResult.Cancel)
                        {
                            return;
                        }

                        TokenResponse tokenResponse = acquireAccessTokenResult.Token;

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV1, Util.ConvertResourceNameToResourceEnum(comboBox_Page03_Resource.SelectedItem.ToString()), textBox_Page03_ClientID.Text, textBox_Page03_ClientSecret.Text, "", textBox_Page03_RedirectUri.Text);
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show(string.Join(Environment.NewLine, validateResult.ErrorMessage), "Office365APIEditor");
                    }                    

                    break;

                case PageIndex.Page04_V1NativeAppOptionForm:
                    // Option form for V1 auth endpoint Native App

                    V1NativeAppUtil v1NativeAppUtil = new V1NativeAppUtil()
                    {
                        TenantName = textBox_Page04_TenantName.Text,
                        ClientID = textBox_Page04_ClientID.Text,
                        RedirectUri = textBox_Page04_RedirectUri.Text,
                        Resource = Util.ConvertResourceNameToResourceEnum(comboBox_Page04_Resource.SelectedItem.ToString())
                    };

                    validateResult = v1NativeAppUtil.Validate();

                    if (validateResult.IsValid)
                    {
                        acquireAccessTokenResult = v1NativeAppUtil.AcquireAccessToken();

                        if (acquireAccessTokenResult.Success == InteractiveResult.Fail)
                        {
                            MessageBox.Show(acquireAccessTokenResult.ErrorMessage, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else if (acquireAccessTokenResult.Success == InteractiveResult.Cancel)
                        {
                            return;
                        }

                        TokenResponse tokenResponse = acquireAccessTokenResult.Token;

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV1, Util.ConvertResourceNameToResourceEnum(comboBox_Page04_Resource.SelectedItem.ToString()), textBox_Page04_ClientID.Text, "", "", textBox_Page04_RedirectUri.Text);
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show(string.Join(Environment.NewLine, validateResult.ErrorMessage), "Office365APIEditor");
                    }                    

                    break;

                case PageIndex.Page05_V1AppOnlyByCertOptionForm:
                    // Option form for V1 auth endpoint Web App App only token by certificate

                    if (ValidateV1WebAppAppOnlyByCertParam())
                    {
                        TokenResponse tokenResponse = AcquireV1WebAppAppOnlyAccessTokenByCert();

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV1, Util.ConvertResourceNameToResourceEnum(comboBox_Page05_Resource.SelectedItem.ToString()), textBox_Page05_ClientID.Text, "", "", "");
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }

                    break;
                case PageIndex.Page06_V2WebAppOptionForm:
                    // Option form for V2 auth endpoint Web App

                    V2WebAppUtil v2WebAppUtil = new V2WebAppUtil()
                    {
                        TenantName = textBox_Page06_TenantName.Text,
                        ClientID = textBox_Page06_ClientID.Text,
                        RedirectUri = textBox_Page06_RedirectUri.Text,
                        Scopes = textBox_Page06_Scopes.Text,
                        ClientSecret = textBox_Page06_ClientSecret.Text
                    };

                    validateResult = v2WebAppUtil.Validate();

                    if (validateResult.IsValid)
                    {
                        acquireAccessTokenResult = v2WebAppUtil.AcquireAccessToken();

                        if (acquireAccessTokenResult.Success == InteractiveResult.Fail)
                        {
                            MessageBox.Show(acquireAccessTokenResult.ErrorMessage, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else if (acquireAccessTokenResult.Success == InteractiveResult.Cancel)
                        {
                            return;
                        }

                        TokenResponse tokenResponse = acquireAccessTokenResult.Token;

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV2, Resources.None, textBox_Page06_ClientID.Text, textBox_Page06_ClientSecret.Text, textBox_Page06_Scopes.Text, textBox_Page06_RedirectUri.Text, textBox_Page06_TenantName.Text);
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show(string.Join(Environment.NewLine, validateResult.ErrorMessage), "Office365APIEditor");
                    }

                    break;

                case PageIndex.Page07_V2MobileAppOptionForm:
                    // Option form for V2 auth endpoint Web App

                    V2NativeAppUtil v2MobileAppUtil = new V2NativeAppUtil()
                    {
                        TenantName = textBox_Page07_TenantName.Text,
                        ClientID = textBox_Page07_ClientID.Text,
                        RedirectUri = textBox_Page07_RedirectUri.Text,
                        Scopes = textBox_Page07_Scopes.Text
                    };

                    validateResult = v2MobileAppUtil.Validate();

                    if (validateResult.IsValid)
                    {
                        acquireAccessTokenResult = v2MobileAppUtil.AcquireAccessToken();

                        if (acquireAccessTokenResult.Success == InteractiveResult.Fail)
                        {
                            MessageBox.Show(acquireAccessTokenResult.ErrorMessage, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else if (acquireAccessTokenResult.Success == InteractiveResult.Cancel)
                        {
                            return;
                        }

                        TokenResponse tokenResponse = acquireAccessTokenResult.Token;

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV2, Resources.None, textBox_Page07_ClientID.Text, "", textBox_Page07_Scopes.Text, textBox_Page07_RedirectUri.Text, textBox_Page07_TenantName.Text);
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show(string.Join(Environment.NewLine, validateResult.ErrorMessage), "Office365APIEditor");
                    }

                    break;

                case PageIndex.Page08_V1AppOnlyByKeyOptionForm:
                    // Option form for V1 auth endpoint Web App App only token by Key

                    if (ValidateV1WebAppAppOnlyByKeyParam())
                    {
                        TokenResponse tokenResponse = AcquireV1WebAppAppOnlyAccessTokenByKey();

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV1, Util.ConvertResourceNameToResourceEnum(comboBox_Page08_Resource.SelectedItem.ToString()), textBox_Page08_ClientID.Text, "", "", "");
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }

                    break;

                case PageIndex.Page09_V1AdminConsentOptionForm:
                    // Option form for V1 Admin Consent

                    if (ValidateV1AdminConsentParam())
                    {
                        string authorizationCode = AcquireV1AdminConsentAuthorizationCode();

                        if (authorizationCode == "")
                        {
                            return;
                        }

                        SaveSettings();
                        MessageBox.Show("Admin Consent completed.", "Office365APIEditor");
                    }
                    break;

                case PageIndex.Page10_BuiltInAppOrBasicAuthSelection:
                    // Mode selection page for built-in app or basic auth.

                    if (radioButton_Page10_BuiltInApp.Checked)
                    {
                        // Built-in app
                        // Go to the next page.
                        ShowPage(PageIndex.Page11_BuiltInAppOptionForm);
                    }
                    else
                    {
                        // Basic auth
                        clientInfo = new ClientInformation(new TokenResponse(), AuthEndpoints.Basic, Resources.None, "", "", "", "");
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                    break;

                case PageIndex.Page11_BuiltInAppOptionForm:
                    // Option form for the built-in app.
                    
                    V2NativeAppUtil builtInAppUtil = new V2NativeAppUtil()
                    {
                        ClientID = Properties.Settings.Default.BuiltInAppClientId,
                        RedirectUri = Properties.Settings.Default.BuiltInAppRedirectUri,
                        Scopes = textBox_Page11_Scopes.Text,
                        TenantName = "common"
                    };

                    validateResult = builtInAppUtil.Validate();

                    if (validateResult.IsValid)
                    {
                        acquireAccessTokenResult = builtInAppUtil.AcquireAccessToken();

                        if (acquireAccessTokenResult.Success == InteractiveResult.Fail)
                        {
                            MessageBox.Show(acquireAccessTokenResult.ErrorMessage, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else if (acquireAccessTokenResult.Success == InteractiveResult.Cancel)
                        {
                            return;
                        }

                        TokenResponse tokenResponse = acquireAccessTokenResult.Token;

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV2, Resources.None, Properties.Settings.Default.BuiltInAppClientId, "", textBox_Page11_Scopes.Text, Properties.Settings.Default.BuiltInAppRedirectUri);
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show(string.Join(Environment.NewLine, validateResult.ErrorMessage), "Office365APIEditor");
                    }

                    break;

                case PageIndex.Page12_V2AppOnlyByPasswordForMicrosoftGraphOptionForm:
                    // Option form for V2 auth endpoint Web App App only token by password for Microsoft Graph

                    if (ValidateV2WebAppAppOnlyByPasswordForMicrosoftGraphParam())
                    {
                        TokenResponse tokenResponse = AcquireV2WebAppAppOnlyAccessTokenByPasswordForMicrosoftGraph();

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV2, Resources.Graph, textBox_Page12_ClientId.Text, textBox_Page12_ClientSecret.Text, textBox_Page12_Scopes.Text, "");
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }

                    break;

                case PageIndex.Page13_V2AdminConsentOptionForm:
                    // Option form for V2 Admin Consent

                    if (ValidateV2AdminConsentParam())
                    {
                        string authorizationCode = AcquireV2AdminConsentAuthorizationCode();

                        if (authorizationCode == "")
                        {
                            return;
                        }

                        SaveSettings();
                        MessageBox.Show("Admin Consent completed.", "Office365APIEditor");
                    }
                    
                    break;

                case PageIndex.Page14_SharePointOnlineAppOnlyOptionForm:
                    // Option form for the SharePoint Online App-Only REST API Microsoft Azure Access Control Service application.

                    if (ValidateSharePointOnlineAppOnlyByKeyParam())
                    {
                        TokenResponse tokenResponse = AcquireSharePointOnlineAppOnlyAccessTokenByKey();

                        if (tokenResponse != null)
                        {
                            SaveSettings();

                            // Create a return value and close this window.
                            clientInfo = new ClientInformation(tokenResponse, AuthEndpoints.OAuthV1, Resources.None, textBox_Page14_ClientID.Text, "", "", "");
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                    }

                    break;

                case PageIndex.None:
                default:
                    break;
            }

            button_Back.Enabled = true;
        }