Skip to content

This sample demonstrates a .NET 5.0 Desktop (WPF) application showcasing advanced Microsoft Graph Query Capabilities for Directory Objects with .NET Graph SDK.

License

Notifications You must be signed in to change notification settings

microsoftgraph/dotnet-aad-query-sample

Repository files navigation

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
csharp
Microsoft Graph
Microsoft identity platform
id displayName
Licantrop0
Luca Spolidoro
ms-graph
dotnet-core
windows-wpf
contentType technologies createdDate
samples
Microsoft Graph
Microsoft identity platform
09/22/2020

Explore advanced Microsoft Graph Query Capabilities for Directory Objects (AAD) with .NET SDK

Overview

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 the ToAsyncEnumerable extension method for an easier pagination.

Prerequisites

Registration

Step 1: Register your application

Use the Microsoft Application Registration Portal to register your application with the Microsoft Graph APIs.
Click New Registration.

Application Registration Note: Make sure to set the right Redirect URI (http://localhost) and application type is Public client/native (mobile & desktop).

Step 2: Set the MS Graph permissions

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.

Api Permissions

Setup

Step 1: Clone or download this repository

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.

Step 2: Configure the ClientId using the Secret Manager

This application use the .NET Core Secret Manager to store the ClientId.
To add the ClientId created on step 1 of registration:

  1. Open a Developer Command Prompt or an Integrated Terminal and locate the dotnet-aad-query-sample\MSGraphSamples.WPF\ directory.
  2. Type dotnet user-secrets set "clientId" "<YOUR CLIENT ID>"

Run the sample

On Visual Studio

Press F5. This will restore the missing nuget packages, build the solution and run the project.

On Visual Studio Code

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

Using the app

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:

Screenshot of the App

  • 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.

Code Architecture

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.

About

This sample demonstrates a .NET 5.0 Desktop (WPF) application showcasing advanced Microsoft Graph Query Capabilities for Directory Objects with .NET Graph SDK.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Languages