問題描述
我正在使用 asp.net 應用程序使用外部身份提供程序 (Azure Active Directory) 進行身份驗證
我想通過 microsoft graph 從 azure ad 獲取組成員
我該怎么做??
似乎您正在嘗試從特定組中獲取所有組成員.只需
代碼片段:
您可以嘗試以下代碼片段,它可以按預期正常工作.
//令牌請求端點字符串 tokenUrl = $"https://login.microsoftonline.com/YourTenant.onmicrosoft.com/oauth2/token";var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl);//我正在使用 client_credentials 作為它主要推薦tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>{["grant_type"] = "client_credentials",["client_id"] = "b6695c7be_YourClient_Id_e6921e61f659",["client_secret"] = "Vxf1SluKbgu4PF0Nf_Your_Secret_Yp8ns4sc=",["資源"] = "https://graph.microsoft.com/"});動態json;AccessTokenClass 結果 = new AccessTokenClass();HttpClient 客戶端 = 新 HttpClient();var tokenResponse = await client.SendAsync(tokenRequest);json = 等待 tokenResponse.Content.ReadAsStringAsync();結果 = JsonConvert.DeserializeObject(json);//用于從 Microsoft Graph Rest API 訪問組成員列表的新塊var groupId = "您要檢索的成員的組 ID";HttpClient _client = new HttpClient();HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("https://graph.microsoft.com/v1.0/groups/{0}/members"),groupId);//為此請求傳遞令牌request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", results.access_token);HttpResponseMessage 響應 = 等待 _client.SendAsync(request);//獲取有商務電話和手機的用戶列表動態 objGpraphUserList = JsonConvert.DeserializeObject<動態>(等待響應.Content.ReadAsStringAsync());
使用的類:
公共類 AccessTokenClass{公共字符串 token_type { 獲取;放;}公共字符串 expires_in { 獲取;放;}公共字符串資源 { 獲取;放;}公共字符串 access_token { 獲取;放;}}
權限:
您需要設置
測試請求結果:
有關更多詳細信息,您可以參考 官方文檔
希望它會有所幫助.如果您遇到任何問題,請隨時分享.
I am working in asp.net application Authenticate with external identity provider (Azure Active Directory)
I want to get group members from azure ad via microsoft graph
How can I do that ??
Seems You are trying to get all group members from a specific group. Just Get the group Id that is Object Id
on azure portal. See the below screen shot.
Code Snippet :
You could try following code snippet which work fine as expected.
//Token Request End Point
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 recommended
tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "client_credentials",
["client_id"] = "b6695c7be_YourClient_Id_e6921e61f659",
["client_secret"] = "Vxf1SluKbgu4PF0Nf_Your_Secret_Yp8ns4sc=",
["resource"] = "https://graph.microsoft.com/"
});
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 Member List from Microsoft Graph Rest API
var groupId = "Group Id which Member You want to Retrieve";
HttpClient _client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("https://graph.microsoft.com/v1.0/groups/{0}/members"),groupId);
//Passing Token For this Request
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", results.access_token);
HttpResponseMessage response = await _client.SendAsync(request);
//Get User List With Business Phones and Mobile Phones
dynamic objGpraphUserList = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());
Class 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; }
}
Permission:
You need to set User.Read.All, Group.Read.All, Directory.Read.All
Application permission
on Microsoft Graph API on azure portal.
Test Request Result:
For more details you could refer to Official Document
Hope it would help. Feel free to share if you encounter any problem.
這篇關于通過 microsoft graph 從 azure ad 獲取組成員的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!