{"id":28524849,"url":"https://github.com/microsoftgraph/dotnet-aad-query-sample","last_synced_at":"2025-07-06T11:32:23.407Z","repository":{"id":55562847,"uuid":"295422462","full_name":"microsoftgraph/dotnet-aad-query-sample","owner":"microsoftgraph","description":"This sample demonstrates a .NET 5.0 Desktop (WPF) application showcasing advanced Microsoft Graph Query Capabilities for Directory Objects with .NET Graph SDK.","archived":false,"fork":false,"pushed_at":"2025-05-27T22:35:00.000Z","size":1360,"stargazers_count":13,"open_issues_count":0,"forks_count":10,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-06-09T11:11:41.974Z","etag":null,"topics":["devxsample","dotnet","dotnet5","graph-explorer","msgraph","msgraphsdk","wpf","wpf-application"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/microsoftgraph.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-14T13:19:58.000Z","updated_at":"2025-05-27T22:35:03.000Z","dependencies_parsed_at":"2023-02-03T04:15:44.842Z","dependency_job_id":"55011d8c-1b31-4811-b2ec-5945c6a33ee8","html_url":"https://github.com/microsoftgraph/dotnet-aad-query-sample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/microsoftgraph/dotnet-aad-query-sample","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoftgraph%2Fdotnet-aad-query-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoftgraph%2Fdotnet-aad-query-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoftgraph%2Fdotnet-aad-query-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoftgraph%2Fdotnet-aad-query-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/microsoftgraph","download_url":"https://codeload.github.com/microsoftgraph/dotnet-aad-query-sample/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoftgraph%2Fdotnet-aad-query-sample/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263892946,"owners_count":23526182,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["devxsample","dotnet","dotnet5","graph-explorer","msgraph","msgraphsdk","wpf","wpf-application"],"created_at":"2025-06-09T11:11:45.104Z","updated_at":"2025-07-06T11:32:23.401Z","avatar_url":"https://github.com/microsoftgraph.png","language":"C#","readme":"---\nuid: dotnet-aad-query-sample\ndescription: Learn how to use .NET Graph SDK to query Directory Objects\npage_type: sample\ncreatedDate: 09/22/2020 00:00:00 AM\nlanguages:\n- csharp\ntechnologies:\n  - Microsoft Graph\n  - Microsoft identity platform\nauthors:\n- id: Licantrop0\n  displayName: Luca Spolidoro\nproducts:\n- ms-graph\n- dotnet-core\n- windows-wpf\nextensions:\n  contentType: samples\n  technologies: \n    - Microsoft Graph\n    - Microsoft identity platform\n  createdDate: 09/22/2020\ncodeUrl: https://github.com/microsoftgraph/dotnet-aad-query-sample\nzipUrl: https://github.com/microsoftgraph/dotnet-aad-query-sample/archive/master.zip\ndescription: \"This sample demonstrates a .NET Desktop (WPF) application showcasing advanced Microsoft Graph Query Capabilities for Directory Objects with .NET\"\n---\n# Explore advanced Microsoft Graph Query Capabilities on Microsoft Entra ID Objects with .NET SDK\n\n- [Overview](#overview)\n- [Prerequisites](#prerequisites)\n- [Registration](#registration)\n  - [Step 1: Register your application](#step-1-register-your-application)\n  - [Step 2: Set the MS Graph permissions](#step-2-set-the-ms-graph-permissions)\n- [Setup](#setup)\n  - [Step 1:  Clone or download this repository](#step-1--clone-or-download-this-repository)\n  - [Step 2: Configure the ClientId using the Secret Manager](#step-2-configure-the-clientid-using-the-secret-manager)\n- [Run the sample](#run-the-sample)\n  - [On Visual Studio](#on-visual-studio)\n  - [On Visual Studio Code](#on-visual-studio-code)\n  - [Using the app](#using-the-app)\n- [Code Architecture](#code-architecture)\n\n## Overview\n\nThis sample helps you explore the Microsoft Graph's [new query capabilities](https://aka.ms/graph-docs/advanced-queries) of the identity APIs using the [Microsoft Graph .NET Client Library v5](https://github.com/microsoftgraph/msgraph-sdk-dotnet) to query Microsoft Entra ID.\nThe main code is in [AsyncEnumerableGraphDataService.cs](MsGraphSamples.Services/AsyncEnumerableGraphDataService.cs) file where, for every request:\n\n- The required `$count=true` QueryString parameter is added\n- The required `ConsistencyLevel=eventual` header is added\n- The request URL is extracted and displayed in the UI\n- The results are converted to an `IAsyncEnumerable` using the [`ToAsyncEnumerable`](MsGraphSamples.Services/AsyncEnumerableGraphDataService.cs#LL34C51-L34C68) extension method for an easier pagination.\n\n## Prerequisites\n\n- Either [Visual Studio (\u003ev16.8)](https://aka.ms/vsdownload) *or* [Visual Studio Code](https://code.visualstudio.com/) with [.NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) and [C# for Visual Studio Code Extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp)\n- A Microsoft Entra ID tenant. For more information, see [How to get an Microsoft Entra ID tenant](https://azure.microsoft.com/documentation/articles/active-directory-howto-tenant/)\n- 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](https://portal.azure.com) with a Microsoft account and have never created a user account in your directory before, you need to do that now.\n\n## Registration\n\n### Step 1: Register your application\n\nUse the [Microsoft Application Registration Portal](https://aka.ms/appregistrations) to register your application with the Microsoft Graph APIs.  \nClick New Registration.\n\n![Application Registration](docs/register_app.png)\n**Note:** Make sure to set the right **Redirect URI** (`http://localhost`) and application type is **Public client/native (mobile \u0026 desktop)**.\n\n### Step 2: Set the MS Graph permissions\n\nAdd the [delegated permissions](https://docs.microsoft.com/graph/permissions-reference#delegated-permissions-20) for `Directory.Read.All`, and grant admin consent.  \nWe advise you to register and use this sample on a Dev/Test tenant and not on your production tenant.\n\n![Api Permissions](docs/api_permissions.png)\n\n## Setup\n\n### Step 1:  Clone or download this repository\n\nFrom your shell or command line:\n\n```Shell\ngit clone https://github.com/microsoftgraph/dotnet-aad-query-sample.git\n```\n\nor download and extract the repository .zip file.\n\n### Step 2: Configure the ClientId using the Secret Manager\n\nThis application use the [.NET Core Secret Manager](https://docs.microsoft.com/aspnet/core/security/app-secrets) to store the **ClientId**.  \nTo add the **ClientId** created on step 1 of registration:\n\n1. Open a **Developer Command Prompt** or an **Integrated Terminal** and locate the `dotnet-aad-query-sample\\MsGraphSamples.Services\\` directory.\n1. Type `dotnet user-secrets set \"clientId\" \"\u003cYOUR CLIENT ID\u003e\"`\n\n## Run the sample\n\n### On Visual Studio\n\nPress F5. This will restore the missing nuget packages, build the solution and run the project.\n\n### On Visual Studio Code\n\nShortly after you open the project folder in VS Code, a prompt by C# extension will appear on bottom right corner:  \n`Required assets to build and debug are missing from 'dotnet-aad-query-sample'. Add them?`.  \nSelect **Yes** and the [C# Dev Kit extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit) should install.  \nUse The Solution Explorer to browse the projects (WinUI or WPF), right-click and debug:\n\n![Solution Explorer](docs/solution_explorer.png)\n\n\u003e Note: Make sure to select `x64` Platform in the lower left corner of the window if you need to build WinUI, as `AnyCPU` is not supported.\n\n### Using the app\n\nIf everything was configured correctly, you should be able to see the login prompt opening in a web browser.  \nThe auth token will be cached in a file for the subsequent runs thanks to [GetBrowserCredential](MsGraphSamples.Services/AuthService.cs#L41C42-L41C62) Method.  \nYou can query your tenant by typing the arguments of the standard OData `$select`, `$filter`, `$orderBy`, `$search` clauses in the relative text boxes.  \nIn the screenshot below you can see the $search operator in action:\n\n![Screenshot of the App](docs/app1.png)\n\n- 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).\n- 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**.\n- If any query error happen, it will displayed with a Message box.\n\nThe generated URL will appear in the readonly Url textbox. You can click the Graph Explorer button to open the current query in Graph Explorer.\n\n## Code Architecture\n\nThis app provides a good starting point for enterprise desktop applications that connects to Microsoft Graph.  \nThe uses [MVVM](https://docs.microsoft.com/windows/uwp/data-binding/data-binding-and-mvvm) pattern and [.NET Community Toolkit](https://github.com/CommunityToolkit/dotnet).  \nThere are two UI projects, one for [WPF](MsGraphSamples.WPF/) and one for [WinUI](MsGraphSamples.WinUI/). The WinUI project implements an advanced technique to iterate all pages of a response using [IAsyncEnumerable](https://learn.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8) and [ISupportIncrementalLoading](https://learn.microsoft.com/uwp/api/windows.ui.xaml.data.isupportincrementalloading).  \nDependency Injection is implemented using [Microsoft.Extensions.DependencyInjection](https://docs.microsoft.com/aspnet/core/fundamentals/dependency-injection).  \n**Nullable** and **Code Analysis** are enabled to enforce code quality.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrosoftgraph%2Fdotnet-aad-query-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicrosoftgraph%2Fdotnet-aad-query-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrosoftgraph%2Fdotnet-aad-query-sample/lists"}