简体   繁体   中英

How to retrieve azure security groups in c#

I am trying to retrieve a list of users from an azure security group, however i am having trouble with this, as i do not know the best possible and easy way to do this in c#. Any help/direction and sample code would be grateful.

You can use Microsoft Graph restful web APIs to access microsoft cloud resources . It has api endpoints for groups , users etc. In your case you can use list groups endpoint to fetch the groups.

https://docs.microsoft.com/en-us/graph/api/resources/group?view=graph-rest-1.0

To retrieve list of users from an azure security group, make sure to grant the below API permission :

在此处输入图像描述

Please try using the below script by Jason Pan in this SO Thread like below:

 public async Task<JsonResult> sample()
    {
        var clientId = Your_Client_ID;
        var clientSecret = Your_Client_Secret;
        var scopes = new[] { "https://graph.microsoft.com/.default" };
        var tenantId = Your_Tenant_ID;
        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
        };
            var clientSecretCredential = new ClientSecretCredential(
            tenantId, clientId, clientSecret, options);
            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
        try
        {
            var members = await graphClient.Groups["Your_Group_ID"].Members.Request().GetAsync();
            return Json(members);
        }
        catch (Exception e)
        {
            return Json("");
            throw;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM