Skip to content

Latest commit

 

History

History
99 lines (60 loc) · 4.32 KB

README.md

File metadata and controls

99 lines (60 loc) · 4.32 KB

How to run the completed project

Prerequisites

To run the completed project in this folder, you need the following:

  • The .NET SDK installed on your development machine. (Note: This tutorial was written with .NET SDK version 7.0.102. The steps in this guide may work with other versions, but that has not been tested.)
  • A Microsoft work or school account with the Global administrator role.

If you don't have a Microsoft account, you can sign up for the Microsoft 365 Developer Program to get a free Microsoft 365 subscription.

Register an application

You can register an application using the Azure Active Directory admin center, or by using the Microsoft Graph PowerShell SDK.

Azure Active Directory admin center

  1. Open a browser and navigate to the Azure Active Directory admin center and login using a Global administrator account.

  2. Select Azure Active Directory in the left-hand navigation, then select App registrations under Manage.

  3. Select New registration. Enter a name for your application, for example, .NET App-Only Graph Tutorial.

  4. Set Supported account types to Accounts in this organizational directory only.

  5. Leave Redirect URI empty.

  6. Select Register. On the application's Overview page, copy the value of the Application (client) ID and Directory (tenant) ID and save them, you will need these values in the next step.

  7. Select API permissions under Manage.

  8. Remove the default User.Read permission under Configured permissions by selecting the ellipses (...) in its row and selecting Remove permission.

  9. Select Add a permission, then Microsoft Graph.

  10. Select Application permissions.

  11. Select User.Read.All, then select Add permissions.

  12. Select Grant admin consent for..., then select Yes to provide admin consent for the selected permission.

  13. Select Certificates and secrets under Manage, then select New client secret.

  14. Enter a description, choose a duration, and select Add.

  15. Copy the secret from the Value column, you will need it in the next steps.

PowerShell

To use PowerShell, you'll need the Microsoft Graph PowerShell SDK. If you do not have it, see Install the Microsoft Graph PowerShell SDK for installation instructions.

  1. Open PowerShell and run the RegisterAppForAppOnlyAuth.ps1 file with the following command.

    .\RegisterAppForAppOnlyAuth.ps1 -AppName ".NET App-Only Graph Tutorial" -GraphScopes "User.Read.All"
  2. Copy the Client ID, Tenant ID, and Client secret values from the script output. You will need these values in the next step.

    SUCCESS
    Client ID: ae2386e6-799e-4f75-b191-855d7e691c75
    Tenant ID: 5927c10a-91bd-4408-9c70-c50bce922b71
    Client secret: ...
    Secret expires: 10/28/2024 5:01:45 PM

Configure the sample

  1. Open appsettings.json and update the values according to the following table.

    Setting Value
    clientId The client ID of your app registration
    tenantId The tenant ID of your organization
  2. Initialize the .NET development secret store by opening your CLI in the directory that contains GraphTutorial.csproj and running the following command.

    dotnet user-secrets init
  3. Add your client secret to the secret store using the following command, replacing <client-secret> with your client secret.

    dotnet user-secrets set settings:clientSecret <client-secret>

    Note: The .NET Secret Manager is only available during development. Production apps should store client secrets in a secure store, such as Azure Key Vault.

Run the sample

In your command-line interface (CLI), navigate to the project directory and run the following commands.

dotnet restore
dotnet build
dotnet run