public async Task<AdApplication> AssureAzureAdAppAndPrincipal(GraphClient cl, Guid tenantId) { AdApplication app = null; ServicePrincipal principal = null; app = await cl.GetApplicationAsync(); if (app != null) principal = await cl.GetServicePrincipalAsync(app.AppId); if (app == null || principal == null) { var r = System.Windows.MessageBox.Show("An application has to be created in your Azure AD. Do you want to continue?", "Please confirm...", System.Windows.MessageBoxButton.OKCancel); if (r == System.Windows.MessageBoxResult.OK) { if (app == null) app = await cl.CreateApplicationAsync(); if (principal == null) { principal = await cl.CreateServicePrincipalAsync(app.AppId); await Task.Factory.StartNew(() => System.Threading.Thread.Sleep(30)); //sleep for 30 secconds so principal is available } } else return null; } return app; }
private async Task RemoveServiceCredential(GraphClient cl, ManagementPack configMp) { var secRefOverride = (ManagementPackSecureReferenceOverride)_emg.Overrides.GetOverrides(new ManagementPackOverrideCriteria($"Name = '{Parameters.SECURE_REFERENCE_OVERRIDE_NAME}'")).FirstOrDefault(); if (secRefOverride != null) { secRefOverride.Status = ManagementPackElementStatus.PendingDelete; secRefOverride.GetManagementPack().AcceptChanges(); } var secData = (BasicCredentialSecureData)_emg.Security.GetSecureData(new SecureDataCriteria($"Name = '{Parameters.SECURE_REFERENCE_NAME}'")).FirstOrDefault(); if (secData != null) _emg.Security.DeleteSecureData(secData); var app = await cl.GetApplicationAsync(); if (app != null && app.PasswordCredentials != null) { var passCred = app.PasswordCredentials.FirstOrDefault(x => x.CustomKeyIdentifier == Convert.ToBase64String(_emg.Id.ToByteArray())); if (passCred != null) { app.PasswordCredentials.Remove(passCred); await cl.UpdateApplicationAsync(app); } } }