Skip to content

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.

License

Notifications You must be signed in to change notification settings

trinitron83/aspnet-webhooks-rest-sample

 
 

Repository files navigation

Microsoft Graph ASP.NET Webhooks

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

This ASP.NET MVC sample shows how to start getting notifications from Microsoft Graph. Microsoft Graph provides a unified API endpoint to access data from the Microsoft cloud.

This sample uses the Azure AD V2 endpoint to obtain an access token for work or school accounts. The sample uses a user-delegated permission, but messages, events, and contacts resources also support application (app-only) permissions. Currently, only drive root item resources support subscriptions for personal accounts. Watch the docs as we continue to add support for these and other features.

The following are common tasks that an application performs with webhooks subscriptions:

  • Get consent to subscribe to users' resources and then get an access token.
  • Use the access token to create a subscription to a resource.
  • Send back a validation token to confirm the notification URL.
  • Listen for notifications from Microsoft Graph and respond with a 202 status code.
  • Request more information about changed resources using data in the notification.

This screenshot shows the app's start page after the user signs in.

Microsoft Graph Webhook Sample for ASP.NET screenshot

After the app creates a subscription on behalf of the signed-in user, Microsoft Graph sends a notification to the registered endpoint when events happen in the user's data. The app then reacts to the event.

This sample subscribes to the me/mailFolders('Inbox')/messages resource for created changes. It gets notified when the user receives an email message, and then updates a page with information about the message.

Prerequisites

To use the Microsoft Graph ASP.NET Webhooks sample, you need the following:

Register the app

This app uses the Azure AD v2 endpoint, so you'll register it in the Application Registration Portal.

  1. Sign in to the portal with either your Microsoft account, or your work or school account.
  2. Choose Add an app.
  3. Enter a friendly name for the application and choose Create application.
  4. Locate the Application Secrets section and choose Generate New Password. Copy the password now and save it to a safe place. Once you've copied the password, click Ok.
  5. Locate the Platforms section, and choose Add Platform. Choose Web, then enter https://localhost:44300 under Redirect URIs.
  6. Choose Save at the bottom of the page.

You'll use the application ID and secret to configure the app in Visual Studio.

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. In Solution Explorer, select the GraphWebhooks project.

  2. Copy the URL port number from the Properties window. If the Properties window isn't showing, choose View > Properties Window.

    The URL port number in the Properties window

  3. Download ngrok for Windows.

  4. Unzip the package and run ngrok.exe.

  5. Replace the two {port-number} placeholder values in the following command with the port number you copied, and then run the command in the ngrok console.

    ngrok http {port-number} -host-header=localhost:{port-number}

    Example command to run in the ngrok console

  6. 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

    Note: 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 Hosting without a tunnel and Why do I have to use a tunnel? for more information.

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. Make a copy of GraphWebHooks/PrivateSettings.example.config in the same directory. Name the copy PrivateSettings.config.

  3. Open GraphWebhooks.sln in the sample files.

    Note: You may be prompted to trust certificates for localhost.

  4. In Solution Explorer, open the PrivateSettings.config file in the root directory of the project.

    • For the ClientId key, replace ENTER_YOUR_APP_ID with the application ID of your registered application.
    • For the ClientSecret key, replace ENTER_YOUR_SECRET with the secret of your registered application.
    • For the NotificationUrl key, replace ENTER_YOUR_URL with the HTTPS URL. Keep the /notification/listen portion. If you're using ngrok, use the HTTPS URL that you copied. The value will look something like this:
    <add key="ida:NotificationUrl" value="https://0f6fd138.ngrok.io/notification/listen" />
  5. Make sure that the ngrok console is still running, then press F5 to build and run the solution in debug mode.

    Note: If you get errors while installing packages, make sure the local path where you placed the solution is not too long/deep. Moving the solution closer to the root drive resolves this issue.

    If you update any dependencies for this sample, do not update System.IdentityModel.Tokens.Jwt to v5, which is designed for use with .NET Core. Also do not update any of the Microsoft.Owin libraries to v4.

Use the app

  1. Sign in with your work or school account.

  2. Consent to the Read your mail and Sign you in and read your profile permissions.

    If you don't see the Read your mail permission, choose Cancel and then add the Read user mail permission to the app in the Azure Portal. See the Register the app section for instructions.

  3. Choose the Create subscription button. The Subscription page loads with information about the subscription.

    Note: This sample sets the subscription expiration to 15 minutes for testing purposes.

    App page showing properties of the new subscription

  4. Choose the Watch for notifications button.

  5. Send an email to your work or school account. The Notification page displays some message properties. It may take several seconds for the page to update.

    App page showing properties of the new message

  6. Choose the Delete subscription and sign out button.

Key components of the sample

Controllers

Models

  • Message.cs Defines the MessageViewModel that represents the data displayed in the Notification view.
  • Notification.cs Represents a change notification.
  • Subscription.cs Defines the SubscriptionViewModel that represents the data displayed in the Subscription view.

Views

Other

  • Web.config Contains values used for authentication and authorization.
  • App_Start/Startup.Auth.cs Contains code used for authentication and authorization. The sample uses OpenID Connect and Microsoft Authentication Library (MSAL) to authenticate and authorize the user.
  • TokenStorage/SampleTokenCache.cs Sample implementation of a token cache that uses System.Runtime.Caching (so that token information is available when a notification is received). Production apps will typically use some method of persistent storage.
  • Helpers/SubscriptionStore.cs Access layer for stored subscription information. The sample implementation temporarily stores the info in HttpRuntime.Cache. Production apps will typically use some method of persistent storage.

Troubleshooting

If you run into errors or problems with the sample, see the troubleshooting doc.

Contributing

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Questions and comments

We'd love to get your feedback about the Microsoft Graph ASP.NET Webhooks sample. 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 Stack Overflow. Make sure that your questions or comments are tagged with MicrosoftGraph.

If you have a feature suggestion, please post your idea on our User Voice page, and vote for your suggestions there.

Additional resources

Copyright

Copyright (c) 2017 Microsoft. All rights reserved.

About

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 92.1%
  • C# 7.8%
  • Other 0.1%