Created
June 23, 2021 12:55
-
-
Save ralacher/724a02adb4c359fdf5f682664487d050 to your computer and use it in GitHub Desktop.
Get Security Groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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