uid | description | page_type | createdDate | languages | technologies | authors | products | extensions | codeUrl | zipUrl | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dotnet-aad-query-sample |
This sample demonstrates a .NET Desktop (WPF) application showcasing advanced Microsoft Graph Query Capabilities for Directory Objects with .NET |
sample |
09/22/2020 00:00:00 AM |
|
|
|
|
|
This sample helps you explore the Microsoft Graph's new query capabilities of the identity APIs using the Microsoft Graph .NET Client Library v5 to query Microsoft Entra ID. The main code is in AsyncEnumerableGraphDataService.cs file where, for every request:
- The required
$count=true
QueryString parameter is added - The required
ConsistencyLevel=eventual
header is added - The request URL is extracted and displayed in the UI
- The results are converted to an
IAsyncEnumerable
using theToAsyncEnumerable
extension method for an easier pagination.
- Either Visual Studio (>v16.8) or Visual Studio Code with .NET 8.0 SDK and C# for Visual Studio Code Extension
- A Microsoft Entra ID tenant. For more information, see How to get an Microsoft Entra ID tenant
- A user account in your Microsoft Entra ID tenant. This sample will not work with a personal Microsoft account (formerly Windows Live account). Therefore, if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now.
Use the Microsoft Application Registration Portal to register your application with the Microsoft Graph APIs.
Click New Registration.
Note: Make sure to set the right Redirect URI (http://localhost
) and application type is Public client/native (mobile & desktop).
Add the delegated permissions for Directory.Read.All
, and grant admin consent.
We advise you to register and use this sample on a Dev/Test tenant and not on your production tenant.
From your shell or command line:
git clone https://github.com/microsoftgraph/dotnet-aad-query-sample.git
or download and extract the repository .zip file.
This application use the .NET Core Secret Manager to store the ClientId.
To add the ClientId created on step 1 of registration:
- Open a Developer Command Prompt or an Integrated Terminal and locate the
dotnet-aad-query-sample\MsGraphSamples.Services\
directory. - Type
dotnet user-secrets set "clientId" "<YOUR CLIENT ID>"
Press F5. This will restore the missing nuget packages, build the solution and run the project.
Shortly after you open the project folder in VS Code, a prompt by C# extension will appear on bottom right corner:
Required assets to build and debug are missing from 'dotnet-aad-query-sample'. Add them?
.
Select Yes and the C# Dev Kit extension should install.
Use The Solution Explorer to browse the projects (WinUI or WPF), right-click and debug:
Note: Make sure to select
x64
Platform in the lower left corner of the window if you need to build WinUI, asAnyCPU
is not supported.
If everything was configured correctly, you should be able to see the login prompt opening in a web browser.
The auth token will be cached in a file for the subsequent runs thanks to GetBrowserCredential Method.
You can query your tenant by typing the arguments of the standard OData $select
, $filter
, $orderBy
, $search
clauses in the relative text boxes.
In the screenshot below you can see the $search operator in action:
- If you double click on a row, a default drill-down will happen (for example by showing the list of transitive groups a user is part of).
- If you click on a header, the results will be sorted by that column. Note: not all columns are supported and you may receive an error.
- If any query error happen, it will displayed with a Message box.
The generated URL will appear in the readonly Url textbox. You can click the Graph Explorer button to open the current query in Graph Explorer.
This app provides a good starting point for enterprise desktop applications that connects to Microsoft Graph.
The uses MVVM pattern and .NET Community Toolkit.
There are two UI projects, one for WPF and one for WinUI. The WinUI project implements an advanced technique to iterate all pages of a response using IAsyncEnumerable and ISupportIncrementalLoading.
Dependency Injection is implemented using Microsoft.Extensions.DependencyInjection.
Nullable and Code Analysis are enabled to enforce code quality.