Skip to content

iqbal-nadiadi/20230315-Microsoft-Learn-Tutorial-Build-dotNET-apps-with-Microsoft-Graph-SDK

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Project name: [Microsoft Learn Tutorial] Build dotNET apps with Microsoft Graph SDK

[MICROSOFT LEARN SOURCE]

Summary: How to run the completed project

  • Please read the README.md on how to run the completed project. Thank you folks in the time permitted. Build and run the sample
  • NOTE: In your command-line interface (CLI), navigate to the project directory and run the following commands. In your command-line interface (CLI), navigate to the project directory and run the following commands.
dotnet restore
dotnet build
dotnet run

Detailed: 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 6.0.102. The steps in this guide may work with other versions, but that has not been tested.)
  • A Microsoft work or school account.

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.

NOTE: If you downloaded this code from https://developer.microsoft.com/graph/quick-start, an app registration has already been created for you. However, if you want to use the app-only portion of this sample, you will need to modify the app registration as specified in Configure app-only auth (AAD admin center) or Configure app-only auth (PowerShell).

Azure Active Directory admin center

  1. Open a browser and navigate to the Azure Active Directory admin center and login using a personal account (aka: Microsoft Account) or Work or School 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 Graph Tutorial.

  4. Set Supported account types as desired. The options are:

    Option Who can sign in?
    Accounts in this organizational directory only Only users in your Microsoft 365 organization
    Accounts in any organizational directory Users in any Microsoft 365 organization (work or school accounts)
    Accounts in any organizational directory ... and personal Microsoft accounts Users in any Microsoft 365 organization (work or school accounts) and personal Microsoft accounts
  5. Leave Redirect URI empty.

  6. Select Register. On the application's Overview page, copy the value of the Application (client) ID and save it, you will need it in the next step. If you chose Accounts in this organizational directory only for Supported account types, also copy the Directory (tenant) ID and save it.

  7. Select Authentication under Manage. Locate the Advanced settings section and change the Allow public client flows toggle to Yes, then choose Save.

Configure app-only auth (AAD admin center)

Note: This section requires a work/school account with the Global administrator role. You only need to complete these steps if you plan on using the app-only portions of this sample.

  1. Select API permissions under Manage.

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

  3. Select Add a permission, then Microsoft Graph.

  4. Select Application permissions.

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

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

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

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

  9. 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 RegisterAppForUserAuth.ps1 file with the following command, replacing <audience-value> with the desired value (see table below).

    Note: The RegisterAppForUserAuth.ps1 script requires a work/school account with the Application administrator, Cloud application administrator, or Global administrator role.

    .\RegisterAppForUserAuth.ps1 -AppName ".NET Graph Tutorial" -SignInAudience <audience-value>
    SignInAudience value Who can sign in?
    AzureADMyOrg Only users in your Microsoft 365 organization
    AzureADMultipleOrgs Users in any Microsoft 365 organization (work or school accounts)
    AzureADandPersonalMicrosoftAccount Users in any Microsoft 365 organization (work or school accounts) and personal Microsoft accounts
    PersonalMicrosoftAccount Only personal Microsoft accounts
  2. Copy the Client ID and Auth tenant values from the script output. You will need these values in the next step.

    SUCCESS
    Client ID: 2fb1652f-a9a0-4db9-b220-b224b8d9d38b
    Auth tenant: common

Configure app-only auth (PowerShell)

Note: This section requires a work/school account with the Global administrator role. You only need to complete these steps if you plan on using the app-only portions of this sample.

  1. Run the UpdateAppForAppOnlyAuth.ps1 file with the following command, replacing <your-client-id> with your client ID.

    .\UpdateAppForAppOnlyAuth.ps1 -AppId <your-client-id> -GraphScopes "User.Read.All"
  2. Copy the Tenant ID and Client secret values from the script output. You will need these values in the next step.

    SUCCESS
    Tenant ID: a795ad0f-7d82-4a3b-a2c0-0713ec72ade7
    Client secret: 2jv7Q~8eiOd_QafJ.....
    Secret expires: 2/16/2024 9:32:09 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 (only needed if doing app-only)
    authTenant If you chose the option to only allow users in your organization to sign in, change this value to your tenant ID. Otherwise leave as common.
  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.

Build and 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

Tags #hack-together and #microsoft-graph-sdk

Hack Together: Microsoft Graph and .NET

About

20230315 [Microsoft Learn Tutorial] Build dotNET apps with Microsoft Graph SDK

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published