private TokenResponse AcquireV1WebAppAppOnlyAccessTokenByKey() { // Build a POST body. string postBody = "grant_type=client_credentials" + "&resource=" + System.Web.HttpUtility.UrlEncode(Util.ConvertResourceNameToUri(comboBox_Page08_Resource.SelectedItem.ToString())) + "&client_id=" + textBox_Page08_ClientID.Text + "&client_secret=" + System.Web.HttpUtility.UrlEncode(textBox_Page08_ClientSecret.Text); return(AcquireAccessToken(postBody, "https://login.microsoftonline.com/" + textBox_Page08_TenantName.Text + "/oauth2/token")); }
private TokenResponse AcquireV1WebAppAccessToken(string AuthorizationCode) { // Build a POST body. string postBody = "grant_type=authorization_code" + "&redirect_uri=" + System.Web.HttpUtility.UrlEncode(textBox_Page03_RedirectUri.Text) + "&client_id=" + textBox_Page03_ClientID.Text + "&client_secret=" + System.Web.HttpUtility.UrlEncode(textBox_Page03_ClientSecret.Text) + "&code=" + AuthorizationCode + "&resource=" + System.Web.HttpUtility.UrlEncode(Util.ConvertResourceNameToUri(comboBox_Page03_Resource.SelectedItem.ToString())); return(AcquireAccessToken(postBody, "https://login.microsoftonline.com/common/oauth2/token")); }
private TokenResponse AcquireV1NativeAppAccessToken(string AuthorizationCode) { // Build a POST body. string postBody = "grant_type=authorization_code" + "&Resource=" + System.Web.HttpUtility.UrlEncode(Util.ConvertResourceNameToUri(comboBox_Page04_Resource.SelectedItem.ToString())) + "&client_id=" + textBox_Page04_ClientID.Text + "&code=" + AuthorizationCode + "&redirect_uri=" + System.Web.HttpUtility.UrlEncode(textBox_Page04_RedirectUri.Text); string endPoint = "https://login.microsoftonline.com/" + textBox_Page04_TenantName.Text.Replace("@", ".") + "/oauth2/token"; return(AcquireAccessToken(postBody, endPoint)); }
private TokenResponse AcquireV1WebAppAppOnlyAccessTokenByCert() { try { FileStream certFile = File.OpenRead(textBox_Page05_CertPath.Text); byte[] certBytes = new byte[certFile.Length]; certFile.Read(certBytes, 0, (int)certFile.Length); var cert = new X509Certificate2(certBytes, textBox_Page05_CertPass.Text, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet); string resource = Util.ConvertResourceNameToUri(comboBox_Page05_Resource.SelectedItem.ToString()); ClientAssertionCertificate cac = new ClientAssertionCertificate(textBox_Page05_ClientID.Text, cert); AuthenticationContext authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/" + textBox_Page05_TenantName.Text, false); AuthenticationResult authenticationResult = null; string errorMessage = null; try { authenticationResult = authenticationContext.AcquireTokenAsync(resource, cac).Result; } catch (AdalException ex) { errorMessage = ex.Message; if (ex.InnerException != null) { errorMessage += "\nInnerException : " + ex.InnerException.Message; } } catch (ArgumentException ex) { errorMessage = ex.Message; } if (!string.IsNullOrEmpty(errorMessage)) { MessageBox.Show(errorMessage, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } else { return(Util.ConvertAuthenticationResultToTokenResponse(authenticationResult)); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } }
private string AcquireV1AdminConsentAuthorizationCode() { GetCodeForm getCodeForm = new GetCodeForm(textBox_Page09_ClientID.Text, textBox_Page09_RedirectUri.Text, Util.ConvertResourceNameToUri(comboBox_Page09_Resource.SelectedItem.ToString()), false, true); if (getCodeForm.ShowDialog(out string Code) == DialogResult.OK) { if (Code == "") { MessageBox.Show("Getting Authorization Code was failed.", "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(Code); }