Getting the list of users from AD is bit tricky. Firstly the call is Asynchronous and it is delayed. So no such straight forward foreach loop can help,
Define the data model
public
class
UserGridData
{
public
string UserId { get; set; }
public
string UserPrincipleName { get; set; }
}
After that we can write something like,
private
ActiveDirectoryClient GetAADClient()
{
Uri serviceRoot = new
Uri(serviceRootURL);
ActiveDirectoryClient adClient = new
ActiveDirectoryClient(
serviceRoot,
async () => await GetAppTokenAsync());
return adClient;
}
private
async
Task GetUsers()
{
List<UserGridData> userData = new
List<UserGridData>();
var adClient = GetAADClient();
var userListTask = adClient.Users.ExecuteAsync();
var userList = await userListTask;
do
{
foreach (var userObject in userList.CurrentPage.ToList())
{
var userValue = (User) userObject;
userData.Add(
new
UserGridData()
{
UserId = userValue.ObjectId,
UserPrincipleName = userValue.UserPrincipalName
}
);
}
} while (userList.MorePagesAvailable);
dataGridView1.DataSource = userData;
}
I have displayed the data in a Grid you may handle it as per your need.
Namoskar!!!