• Stars
    star
    117
  • Rank 291,182 (Top 6 %)
  • Language
    C#
  • License
    MIT License
  • Created about 7 years ago
  • Updated 23 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This sample shows how to subscribe for Microsoft Graph webhooks using application (app-only) permissions and the Azure AD endpoint.
page_type description products languages extensions
sample
Create Microsoft Graph webhook subscriptions for an ASP.NET Core app, so that it can receive notifications of changes for any resource. This sample also supports receiving change notifications with data, validating and decrypting the payload.
ms-graph
microsoft-graph-mail-api
microsoft-graph-teamwork-api
microsoft-graph-change-notifications-api
office-exchange-online
azure-active-directory
office-teams
aspx-csharp
csharp
contentType technologies services createdDate
samples
Microsoft Graph
Microsoft Teams
Azure AD
Office 365
Change notifications
3/3/2017 8:55:02 AM

Microsoft Graph Change Notifications Sample for ASP.NET Core

.NET Core

Subscribe for Microsoft Graph change notifications to be notified when your user's data changes, so you don't have to poll for changes.

This sample ASP.NET Core web application shows how to subscribe for change notifications as well as how to validate and decrypt change notifications with resource data when supported by the resource.

This sample uses:

Using the Microsoft Graph Webhooks Sample

This sample implements the following scenarios.

  • Uses user-delegated authentication to subscribe to notifications in a user's Exchange Online inbox.
  • Uses app-only authentication to subscribe to notifications for all new Teams channel messages. These notifications include encrypted resource data.

User-delegated authentication represents a user and the application being used when calling the Microsoft Graph. This type of authentication is best suited for scenarios when the user interacts with the application. Application only authentication represents only the application itself when calling the Microsoft Graph, without any notion of user. This type of authentication is best suited for background services, daemons or other kind of applications users are not directly interacting with.

See the list of permissions and authentication types permitted for each supported resource in Microsoft Graph.

Prerequisites

To use the Microsoft Graph Webhook Sample for ASP.NET Core, you need the following:

  • .NET 5.0 or later.
  • A work, school or personal account. Note: Personal accounts cannot access the app-only Teams channel scenario in the app. A tenant administrator account is required to grant application permissions for the app-only portion.
  • The application ID and secret from the application that you register on the Azure Portal.
  • A public HTTPS endpoint to receive and send HTTP requests. You can host this on Microsoft Azure or another service, or you can use ngrok or a similar tool while testing.
  • If you are also testing change notifications with resource data, you also need a Microsoft Azure subscription to create an Azure Key Vault. If you do not have a Microsoft Azure subscription, you can start a free trial.

Create the app registration

Choose the tenant where you want to create your app

  1. Sign in to the Azure portal using either a work or school account.
  2. If your account is present in more than one Azure AD tenant:
    1. Select your profile from the menu on the top right corner of the page, and then Switch directory.
    2. Change your session to the Azure AD tenant where you want to create your application.

Register the app

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

    A screenshot of the App registrations

  2. Select New registration. On the Register an application page, set the values as follows.

    • Set Name to ASP.NET Graph Notification Webhook Sample.
    • Set Supported account types to Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts.
    • Under Redirect URI, set the first drop-down to Web and set the value to https://localhost:5001/.

    A screenshot of the Register an application page

  3. Select Register to create the app. On the app's Overview page, copy the value of the Application (client) ID and Directory (tenant) ID and save them for later.

  4. Select Authentication under Manage. Add an additional Redirect URI with the value https://localhost:5001/signin-oidc.

  5. Set the Front-channel logout URL to https://localhost:5001/signout-oidc. Select Save.

  6. Select Certificates & secrets under Manage. Select the New client secret button. Enter a value in Description and select one of the options for Expires and select Add.

  7. Copy the Value of the new secret before you leave this page. It will never be displayed again. Save the value for later.

    A screenshot of a new secret in the Client secrets list

  8. Select API permissions under Manage.

  9. In the list of pages for the app, select API permissions, then select Add a permission.

  10. Make sure that the Microsoft APIs tab is selected, then select Microsoft Graph.

  11. Select Application permissions, then find and enable the ChannelMessage.Read.All permission. Select Add permissions to add the enabled permission.

    Note: To create subscriptions for other resources you need to select different permissions as documented here

  12. In the Configured permissions list, select the ellipses (...) in the User.Read row, and select Remove permission. The User.Read permission will be requested dynamically as part of the user sign-in process.

    A screenshot of the Remove permission menu item

  13. Select Grant admin consent for name of your organization and Yes. This grants consent to the permissions of the application registration you just created to the current organization.

Setting up Azure Key Vault

Note: The app-only scenario in this sample requires this step. It assumes that the public/private key pair for encrypting and decrypting resource data in the notification payloads are stored in Azure Key Vault. Refer to the documentation for a complete list of resources that support including resources data.

Follow the documented steps to configure your Azure KeyVault in order to add support for change notifications with resource data.

Set up the ngrok proxy (optional)

You must expose a public HTTPS endpoint to create a subscription and receive notifications from Microsoft Graph. While testing, you can use ngrok to temporarily allow messages from Microsoft Graph to tunnel to a localhost port on your computer.

You can use the ngrok web interface (http://127.0.0.1:4040) to inspect the HTTP traffic that passes through the tunnel. To learn more about using ngrok, see the ngrok website.

  1. Download ngrok.

  2. Run the following command in your command-line interface.

    ngrok http https://localhost:5001
  3. Copy the HTTPS URL that's shown in the console. You'll use this to configure your notification URL in the sample.

    The forwarding HTTPS URL in the ngrok console

Keep the console open while testing. If you close it, the tunnel also closes and you'll need to generate a new URL and update the sample.

See troubleshooting for more information about using tunnels.

Configure and run the sample

  1. Expose a public HTTPS notification endpoint. It can run on a service such as Microsoft Azure, or you can create a proxy web server by using ngrok or a similar tool.

  2. Open appsettings.json in the root directory of the project.

    Note: During development, it's recommended that you use the .NET Secret Manager to store secrets instead of putting them in appsettings.json.

    • Settings under AzureAd:
      • TenantId: Your tenant ID (obtained when registering the application)
      • ClientId: Your application ID (obtained when registering the application)
      • ClientSecret: Your client secret (obtained when registering the application) RECOMMENDED: set this in Secret Manager instead: dotnet user-secrets set AzureAd:ClientSecret "YOUR_CLIENT_SECRET".
    • GraphScopes: The Graph permission scopes used in the user-delegated scenario. These are already set for the user's inbox scenario. You'll need to change these if you subscribe to a different resource.
    • NotificationHost: Set to the host name of the server that hosts your application. During local development, set this to your ngrok URL.
    • Settings under KeyVault:
      • Url: The URL to your Azure Key Vault
      • CertificateName The name of the certificate in your Azure Key Vault
  3. Make sure that the ngrok console is still running, then run the app with the following command.

    dotnet run

    Note: You can use Visual Studio Code to set breakpoints and run the sample in debug mode.

Use the app to create a subscription

Use delegated authentication to subscribe to a user's inbox

  1. Choose the Sign in and subscribe button and sign in with a work or school account.

  2. Review and consent to the requested permissions. The subscription is created and you are redirected to a page displaying any notification being received.

  3. Send an email to yourself. A notification appears showing the subject and message ID.

    A screenshot of the user inbox notifications page

Use app-only authentication to subscribe to Teams channel messages

  1. If you previously subscribed to a user's inbox, choose the Delete subscription button to return to the home page.

  2. Choose the Subscribe button. The subscription is created and you are redirected to a page displaying any notification being received.

  3. Post a message to a channel in any team in Microsoft Teams. A notification appears showing the sender's name and the message.

    A screenshot of the Teams channel notifications page

Troubleshooting

See the dedicated troubleshooting page.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Questions and comments

We'd love to get your feedback about the Microsoft Graph Webhooks sample for ASP.NET Core. You can send your questions and suggestions to us in the Issues section of this repository.

Questions about Microsoft Graph in general should be posted to Microsoft Q&A. Make sure that your questions or comments are tagged with the relevant Microsoft Graph tag.

Additional resources

More Repositories

1

powershell-intune-samples

This repository of PowerShell sample scripts show how to access Intune service resources. They demonstrate this by making HTTPS RESTful API requests to the Microsoft Graph API from PowerShell.
PowerShell
1,225
star
2

microsoft-graph-docs

Documentation for the Microsoft Graph REST API
PowerShell
1,212
star
3

microsoft-graph-toolkit

Authentication Providers and UI components for Microsoft Graph ๐Ÿฆ’
TypeScript
899
star
4

msgraph-sdk-javascript

Microsoft Graph client library for JavaScript
TypeScript
664
star
5

msgraph-sdk-powershell

Powershell SDK for Microsoft Graph
C#
651
star
6

msgraph-sdk-dotnet

Microsoft Graph Client Library for .NET!
C#
644
star
7

msgraph-sdk-php

Microsoft Graph Library for PHP.
PHP
484
star
8

msgraph-sdk-java

Microsoft Graph SDK for Java
Java
353
star
9

msgraph-sdk-python

Python
280
star
10

microsoftgraph-postman-collections

253
star
11

msgraph-sdk-python-core

Microsoft Graph client library for Python
Python
252
star
12

security-api-solutions

Microsoft Graph Security API applications and services.
Jupyter Notebook
208
star
13

msgraph-sdk-go

Microsoft Graph SDK for Go
Go
196
star
14

microsoft-graph-explorer-v4

React/Redux version of Graph Explorer used to learn the Microsoft Graph Api
TypeScript
196
star
15

microsoft-graph-comms-samples

Microsoft Graph Communications Samples
196
star
16

aspnet-snippets-sample

A repository of code snippets that use Microsoft Graph to perform common tasks such as sending email, managing groups, and other activities from an ASP.NET Core MVC app. This sample uses the Microsoft Graph .NET Client Library to work with data, and the Microsoft Identity Web Library for authentication on the Microsoft identity platform v2.0 endpoint.
C#
177
star
17

msgraph-sample-nodeexpressapp

This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from Node.js Express apps.
JavaScript
158
star
18

msgraph-sdk-dotnet-core

The core Microsoft Graph client library for .Net. (Microsoft.Graph.Core)
C#
138
star
19

python-sample-auth

Please see https://github.com/microsoftgraph/msgraph-training-pythondjangoapp for a Python sample.
Python
136
star
20

microsoft-graph-explorer

We've moved! You can contribute to our new repository here
TypeScript
136
star
21

msgraph-sample-phpapp

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from PHP web apps.
PHP
133
star
22

nodejs-webhooks-sample

Create Microsoft Graph webhook subscriptions for a Node.js app, so that it can receive notifications of changes in a userโ€™s Microsoft account data.
JavaScript
128
star
23

msgraph-sample-pythondjangoapp

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from Python web apps.
Python
123
star
24

aspnetcore-connect-sample

[ARCHIVED] This ASP.NET Core MVC sample shows how to connect to Microsoft Graph using delegated permissions and the Azure AD v2.0 (MSAL) endpoint.
C#
123
star
25

msgraph-sample-reactspa

This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from React browser apps.
TypeScript
115
star
26

csharp-teams-sample-graph

[ARCHIVED] Sample apps (C# and node) for accessing Microsoft Teams data via the Microsoft Graph
JavaScript
115
star
27

msgraph-training-dotnet

Microsoft Graph Training Module - Build .NET apps with Microsoft Graph
C#
107
star
28

python3-connect-rest-sample

[ARCHIVED] This sample shows how to connect your Python web app to Office 365 using Microsoft Graph to send an email.
Python
106
star
29

nodejs-connect-rest-sample

[ARCHIVED] Use Microsoft Graph to access a userโ€™s Microsoft account data from within a Node.js web application. This sample sends REST calls directly to the Microsoft Graph endpoint, and authenticates with the Azure AD v2.0 endpoint to support Microsoft Accounts and work or school accounts.
JavaScript
100
star
30

msgraph-typescript-typings

Microsoft Graph TypeScript Type Definitions
PowerShell
99
star
31

MSGraph-SDK-Code-Generator

MSGraph SDK Code Generator
C#
88
star
32

msgraph-training-aspnetmvcapp

Microsoft Graph Training Module - Build MVC apps with Microsoft Graph
88
star
33

msgraph-metadata

Microsoft Graph metadata captured and used for generating client library code files.
XSLT
87
star
34

msgraph-beta-sdk-dotnet

The Microsoft Graph Client Beta Library for .NET supports the Microsoft Graph /beta endpoint. (preview)
C#
86
star
35

microsoft-graph-devx-api

Services API for Graph Explorer
C#
84
star
36

msgraph-sdk-ruby

Microsoft Graph Ruby client library for v1 APIs
Ruby
83
star
37

dotnetcore-console-sample

.Net Core console application for calling Microsoft Graph. Sample built from scratch.
C#
83
star
38

python-security-rest-sample

This sample shows how to connect your Python web app to the Security API using Microsoft Graph.
HTML
79
star
39

30DaysMSGraph-TryItOut

This repo contains the Try It Out samples from the 30 Days of Microsoft Graph blog series (https://aka.ms/30DaysMSGraph).
C#
78
star
40

php-connect-rest-sample

[ARCHIVED] Use Microsoft Graph to access a userโ€™s Microsoft account data from within a php web application. This sample gets OAuth tokens from the Azure AD v2.0 endpoint and uses REST calls directly to Microsoft Graph.
PHP
78
star
41

msgraph-sdk-dotnet-auth

Archived - use the TokenCredential classes provided by Azure.Identity. https://docs.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme
C#
78
star
42

msgraph-cli

CLI tool for Microsoft Graph based on .NET
C#
77
star
43

graphql-demo

[ARCHIVED] GraphQL for Microsoft Graph
JavaScript
77
star
44

msgraph-sample-rubyrailsapp

This sample demonstrates how to use the Microsoft Graph REST API to access data in Office 365 from Ruby on Rails apps.
Ruby
76
star
45

msgraph-sdk-design

Requirements and design documents for Microsoft Graph SDKs
SCSS
70
star
46

python-sample-console-app

Please see https://github.com/microsoftgraph/msgraph-training-pythondjangoapp for a Python sample.
Python
66
star
47

contoso-airlines-teams-sample

JavaScript
65
star
48

aspnet-connect-rest-sample

[ARCHIVED] This ASP.NET MVC sample shows how to connect to Microsoft Graph and use the API to send an email.
C#
63
star
49

msgraph-sample-angularspa

This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from Angular single-page applications.
TypeScript
62
star
50

aspnet-security-api-sample

This sample shows how to connect your ASP .Net web app to the Security API using Microsoft Graph SDK.
JavaScript
59
star
51

msgraph-sample-aspnet-core

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from ASP.NET Core apps.
C#
57
star
52

aspnet-connect-sample

[ARCHIVED] This walkthrough shows you how to use the Office 365 Connected Services in Visual Studio 2017.
C#
54
star
53

msgraph-community-samples

Repo for community contributed samples
53
star
54

php-connect-sample

[ARCHIVED] Use Microsoft Graph to access a userโ€™s Microsoft account data from within a PHP web application. This sample gets OAuth tokens from the Azure AD v2.0 endpoint and connects it to the Microsoft Graph SDK for PHP.
PHP
52
star
55

msgraph-sdk-android

Microsoft Graph SDK for Android! https://graph.microsoft.io
Java
51
star
56

msgraph-training-changenotifications

50
star
57

msgraph-training-python

Completed project for Build Python apps with Microsoft Graph
Python
50
star
58

msgraph-training-authentication

[ARCHIVED] Microsoft Graph Training Module - Authentication
49
star
59

msgraph-training-java

Microsoft Graph Training Module - Build Java apps with Microsoft Graph
Java
49
star
60

msgraph-sdk-java-core

Microsoft Graph SDK for Java - Core Library
Java
49
star
61

angular-connect-rest-sample

[ARCHIVED] Use Microsoft Graph to access a userโ€™s Microsoft account data from within an AngularJS web application. This sample uses REST calls directly to the Microsoft Graph endpoint and authenticates users with Microsoft Accounts or work or school Office 365 accounts.
JavaScript
48
star
62

nodejs-apponlytoken-rest-sample

[ARCHIVED] This sample shows how to connect a service or daemon app (written in Node.js) to the Microsoft Graph API.
JavaScript
47
star
63

msgraph-cli-archived

The Microsoft Graph CLI repository has moved. This repository is now an archive.
Python
44
star
64

dataconnect-solutions

Microsoft Graph Data Connect enables scaled access to Microsoft 365 data with fine grained consent model, integrated with compliance and governance, to enable analytics and big data applications on the largest productivity platform.
Jupyter Notebook
44
star
65

msgraph-sample-azurefunction-csharp

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from Azure Functions.
C#
43
star
66

group-membership-management

Group Membership Management (GMM) is a service that dynamically manages the membership of AAD Groups. Groups managed by GMM can have their membership defined using existing AAD Groups and/or custom membership sources.
C#
43
star
67

aspnet-webhooks-rest-sample

Webhooks in Microsoft Graph notify your web application about changes in user data. You can create a webhook subscription to get notified about changes in user's data. This sample uses the Azure AD endpoint to obtain an access token for work or school accounts.
JavaScript
42
star
68

msgraph-sample-react-native

This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from React Native mobile apps.
TypeScript
42
star
69

xamarin-csharp-propertymanager-sample

[ARCHIVED] Demonstrates how to use the Microsoft Graph as the only back-end component for a complete property management solution.
C#
38
star
70

java-spring-webhooks-sample

Receive change notifications with data with the Microsoft Graph
Java
37
star
71

msgraph-sdk-objc

Microsoft Graph SDK for Objective-C
Objective-C
37
star
72

msgraph-search-connector-sample

This .NET Core application shows how to use the Microsoft Graph indexing API to create a connection to the Microsoft Search service and index custom items.
C#
37
star
73

msgraph-sample-android

This sample demonstrates how to use the Microsoft Graph Java SDK to access data in Office 365 from native mobile Android applications.
Java
37
star
74

console-csharp-connect-sample

[ARCHIVED] This sample shows how to connect a Windows console application to a Microsoft work or school (Azure Active Directory) or personal (Microsoft) account using the Microsoft Graph API to send an email.
C#
36
star
75

angular2-connect-rest-sample

[ARCHIVED] To see a current sample that demonstrates the concepts explained here, visit https://github.com/microsoftgraph/angular4-connect-sample
TypeScript
35
star
76

msgraph-sample-spfx

This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from SharePoint Framework (SPFX) applications.
JavaScript
34
star
77

msgraph-sdk-java-auth

Authentication Providers for Microsoft Graph Java SDK
Java
34
star
78

ruby-connect-rest-sample

[ARCHIVED] Use Microsoft Graph to access a userโ€™s Microsoft account resources from within a Ruby on Rails web application. This sample uses REST calls directly to Microsoft Graph, and uses the Azure AD v2.0 endpoint for authentication of users' work or school Office 365 accounts.
Ruby
34
star
79

meetings-capture-sample

A Microsoft Teams solution that helps teams capture meeting minute notes inside individual channels
HTML
33
star
80

microsoft-graph-docs-contrib

Documentation for the Microsoft Graph REST API
PowerShell
32
star
81

msgraph-sdk-dotnet-contrib

Community contributed extensions to the Microsoft Graph .NET SDK service library, to support APIs that have not yet made it into Microsoft Graph
C#
32
star
82

msgraph-sample-uwp

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from UWP apps.
C#
30
star
83

contoso-airlines-azure-functions-sample

[ARCHIVED]
C#
29
star
84

msgraph-samples-dashboard

Dashboard for msgraph samples
C#
29
star
85

botframework-csharp-excelbot-rest-sample

[ARCHIVED] Excel Bot is a bot built with the Microsoft Bot Framework that demonstrates how to use Excel with the Microsoft Graph API
C#
29
star
86

msgraph-sample-maui

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from .NET MAUI apps.
C#
28
star
87

msgraph-training-building-apps

[ARCHIVED] Microsoft Graph Training Module - Building Apps
28
star
88

uwp-csharp-connect-sample

[ARCHIVED] This sample shows how to connect your Windows 10 Universal app to Office 365 using the Microsoft Graph .NET client library to send an email.
C#
28
star
89

uwp-csharp-snippets-sample

[ARCHIVED] This sample shows how to use the Microsoft Graph SDK to send email, manage groups, and perform other activities with Office 365 data.
C#
26
star
90

python-sample-send-mail

[ARCHIVED] send email from Python via Microsoft Graph
Python
26
star
91

msgraph-sdk-ios

[ARCHIVED] Microsoft Graph SDK for iOS
Objective-C
24
star
92

react-officeuifabric-sample

[ARCHIVED] This sample demonstrates how to populate Office UI Fabric React components with data retrieved from Microsoft Graph.
JavaScript
24
star
93

msgraph-training-powerautomate

Microsoft Graph Training Module - Create a Microsoft Graph JSON Batch Custom Connector for Microsoft Power Automate & Azure Logic Apps
23
star
94

msgraph-sample-javascriptspa

This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from JavaScript browser apps.
JavaScript
23
star
95

msgraph-sample-blazor-clientside

This sample demonstrates how to use the Microsoft Graph .NET SDK to access data in Office 365 from Blazor WebAssembly apps.
HTML
23
star
96

msgraph-training-dataconnect

22
star
97

aspnet-todo-rest-sample

This sample shows how to read and write into an Excel document stored in your OneDrive for Business account by using the Excel REST APIs.
C#
22
star
98

android-java-snippets-sample

Code snippets that use the Microsoft Graph to perform common tasks, such as sending email or managing groups, from within an Android app. This sample uses the Microsoft Graph Client Library to work with the data, and the Microsoft Authentication Library (MSAL) for authentication of users' work or school Office 365 accounts and personal accounts.
Java
22
star
99

msgraph-sdk-raptor

End to end testing tool for verifying generated code snippets and testing against Graph APIs
C#
21
star
100

console-java-connect-sample

Please see https://github.com/microsoftgraph/msgraph-training-java for a Java sample.
Java
21
star