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 Azure AD. 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 7.0 SDK and C# for Visual Studio Code Extension
- An Azure Active Directory (Azure AD) tenant. For more information, see How to get an Azure AD tenant
- A user account in your Azure AD 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.WPF\
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 a .vscode
folder will be created in the project root folder.
Once this is done, open an integrated terminal in VS Code, then type:
cd MSGraphSamples.WPF
dotnet build
dotnet run
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.
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 implementation is a classic WPF MVVM app with Views, ViewModels and Services, using .NET Community Toolkit as MVVM framework.
Dependency Injection is implemented using Microsoft.Extensions.DependencyInjection, supporting design-time data.
Nullable and Code Analysis are enabled to enforce code quality.