Provides a simple .NET 8 based console app implementation of the Microsoft Graph SDK which shows authentication and retrieval of a user's OneDrive root drive.
This sample originated while trying to learn how to create an application that uses the Microsoft Graph to access OneDrive (see Getting started with OneDrive API)
After following the quickstart tutorial I had a functional prototype that authenticated to the graph using the MSAL library, but the implementation felt heavy and did not illustrate how to encapsulate the login functionality into the .NET dependency injection system.
More research turned up the following two pages which this example utilized to implment authentication into a dependency injected service along with the GraphServiceClient so all that is necessary to make a class capable of calling the Micrsoft Graph is to inject the GraphServiceClient class into the constructor.
-
Token caching in the Azure Identity client library - explained how to authenticate
-
Token caching in the Azure Identity client library - Made it possible to serialize the token credential to a disk file so you can avoid having to login when the app is loaded later. The code automatically handles displaying the Microsoft authentication Web UI whenever login is required.
To register your application and add the app's registration information to your solution manually, follow these steps:
-
Sign in to the Microsoft Entra admin center.
-
If you have access to multiple tenants, use the Settings icon
in the top menu to switch to the tenant in which you want to register the application from the Directories + subscriptions menu. -
Browse to Identity > Applications > App registrations, select New registration.
-
Enter a Name for your application, for example My Application. Users of your app might see this name, and you can change it later.
-
In the Supported Account Types section, select Accounts in any identity provider or organizational directory (for authenticating users with user flows).
-
In the Redirect URI (recommended) section, select Public client/native (mobile & desktop) and enter
http://localhostas the redirect uri. -
Under the Permissions section, keep the Grant admin consent to openid and offline_access permissions checkbox checked.
-
Click the Register button to create the application.
-
In the appsettings.json file, set the
ClientIdproperty to the value shown in the Application (client) ID field under the Overview tab in the app registration. -
Run the project.
- It should prompt you to login.
- Login should complete successfully.
- A file named
token.cachewill be saved in the application bin directory which will be used on subsequent application loads. - Subsequent loads should not require login until the current login expires. At that point the browser should show the login screen to allow you to login. this is all handled inside the Azure.Identity pacakage in conjunction with the web browser.
- You can delete the token.cache file to require login again.
- The current implementation does not include Sign out logic.
- The current implmementation will not recognize scopes being changed in the configuration file which would require login to be re-done.
- You can delete the token cache file to get around this for now.