public async Task <Application> CreateApp(string appName, AadAuthorityAudience audience) { var application = new Application { DisplayName = appName, SignInAudience = audience.ToString() }; var createdApp = await _graphClient?.Applications .Request() .AddAsync(application); return(createdApp); }
public async Task <string> CreateApp(string appName, string tenantId, AadAuthorityAudience audience) { var graphUrl = "https://graph.microsoft.com/beta"; var createAppUri = "applications"; var serializedApp = JsonConvert.SerializeObject(new { displayName = appName, signInAudience = audience.ToString() }); var accessToken = await GetAccessToken(scopesToCreateApp, tenantId); string result = string.Empty; try { using (var httpClient = new HttpClient()) { httpClient.BaseAddress = new Uri($"{graphUrl}/"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var response = await httpClient.PostAsync(createAppUri, new StringContent(serializedApp, Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { var content = response.Content; result = await content.ReadAsStringAsync(); } else { result = await response.Content.ReadAsStringAsync(); // TODO WTS: Please handle other status codes as appropriate to your scenario } } } catch (Exception ex) { result = $"Error to create app: {ex.Message}"; } return(result); }