Skip to content

Instantly share code, notes, and snippets.

@ralacher
Created June 23, 2021 12:55
Show Gist options
  • Save ralacher/724a02adb4c359fdf5f682664487d050 to your computer and use it in GitHub Desktop.
Save ralacher/724a02adb4c359fdf5f682664487d050 to your computer and use it in GitHub Desktop.
Get Security Groups
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Microsoft.Graph.Auth;
namespace GetGroups
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string objectId = req.Query["objectId"];
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(Environment.GetEnvironmentVariable("ClientId"))
.WithTenantId(Environment.GetEnvironmentVariable("TenantId"))
.WithClientSecret(Environment.GetEnvironmentVariable("ClientSecret"))
.Build();
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient client = new GraphServiceClient(authenticationProvider);
log.LogInformation($"Object ID {objectId}");
var groups = await client.Users[objectId].MemberOf.Request().GetAsync();
List<string> results = new List<string>();
foreach (var group in groups)
{
log.LogInformation($"Group {group.Id}");
results.Add(group.Id);
}
JObject j = new JObject();
j.Add("groups", JToken.FromObject(results));
log.LogInformation(j.ToString());
return new OkObjectResult(j);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment