Authenticate MS Graph API Using AppId In Dot net SDK
In this article, we learn about how to do simple authentication with the MS Graph(Microsoft Graph) API in DotNet or C# Conole App.
Microsoft Graph is a Unified API. It is a Microsoft developer platform that connects multiple services and devices. Initially released in 2015, the Microsoft Graph builds on Office 365 APIs and allows developers to integrate their services with Microsoft products, including Windows, Office 365, Azure.
Microsoft Graph is a Unified API meaning that single access token created using Microsoft app registration can be used with different service and it is CORS enabled, So No More issue in Browser(CORS issue might be you have faced using Sharepoint REST Request).
Recently, I came across a scenario to build a console app in which we have to Integrate Microsoft Graph API using C#.
To Request Microsoft graph API from a console app, we need utility helper as below which is responsible for preparing all required configuration and setup GraphClient based on ClientId, Scope.
I have searched a lot for the running sample for MS Graph SDK but most of them didn't work for me. It's an older version of SDK which is not working. I have found this code from Github repo and updated with the latest SDK,
//Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
//See LICENSE in the project root for license information.
// The Client ID is used by the application to uniquely identify itself to the v2.0 authentication endpoint.
foreach (var user in IdentityClientApp.GetAccountsAsync().Result)
graphClient = null;
TokenForUser = null;
Before requesting graph API it will ask for authentication info; e.g. Modal popup from Microsoft. Once you enter valid credential a dialog will automatically be closed and you can see a graph request result as per below.
staticvoid Main(string args)
var Users = graphClient.Users.Request().Top(400).GetAsync().Result;