Reputation: 23
I am new to azure web app my use case is to display all the users present who belong to a single AD group in a web page. I have already tried to run the power shell command "Get-azureaduser" in my webapp controller but it is throwing me an error stating the "poweshell workspace has to be run in admin mode". Any help is appreciated.
Upvotes: 0
Views: 793
Reputation: 22515
You can try below code snippet using Microsoft Graph SDK
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var groups = await graphClient.Groups
.Request()
.GetAsync();
Another way you could try:
string tokenUrl = $"https://login.microsoftonline.com/YourTenant.onmicrosoft.com/oauth2/token";
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
//I am Using client_credentials as It is mostly recomended
tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "client_credentials",
["client_id"] = "b603c7be-a866_Your_Client_Id_6921e61f925",
["client_secret"] = "Vxf1SluKbgu_Client_Secret_SeZ8wL/Yp8ns4sc=",
["resource"] = "https://graph.microsoft.com/" // If you use auth/V2.0 then use ["scope"] = "https://graph.microsoft.com/.default"
});
dynamic json;
AccessTokenClass results = new AccessTokenClass();
HttpClient client = new HttpClient();
var tokenResponse = await client.SendAsync(tokenRequest);
json = await tokenResponse.Content.ReadAsStringAsync();
results = JsonConvert.DeserializeObject<AccessTokenClass>(json);
//New Block For Accessing Group Data from Microsoft Graph Rest API
HttpClient _client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("https://graph.microsoft.com/v1.0/groups"));
//Passing Token For this Request
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", results.access_token);
HttpResponseMessage response = await _client.SendAsync(request);
dynamic objAdGroupList = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());
Class I have used:
public class AccessTokenClass
{
public string token_type { get; set; }
public string expires_in { get; set; }
public string resource { get; set; }
public string access_token { get; set; }
}
Required permission On Azure Portal:
You should have
Application permission
Group.Read.All, Directory.Read.All, Group.ReadWrite.AllDirectory.ReadWrite.All
permission on azure portal.
See the screenshot below:
If you still have any concern please refer to official docs and feel free to share.
Hope it would help
Upvotes: 1
Reputation: 222722
You can use the Graph API list group method
GET https://graph.microsoft.com/v1.0/groups
https://learn.microsoft.com/en-us/graph/api/group-list?view=graph-rest-1.0&tabs=http
Upvotes: 0