Git Product home page Git Product logo

fjsnogueira / aspnet-webhooks-rest-sample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from microsoftgraph/aspnet-webhooks-rest-sample

0.0 3.0 0.0 1.19 MB

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 Office 365 data.

License: MIT License

C# 23.39% CSS 0.29% ASP 0.06% JavaScript 76.25%

aspnet-webhooks-rest-sample's Introduction

Microsoft Graph ASP.NET Webhooks

Subscribe for webhooks to get 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.

The following are common tasks that a web application performs with Microsoft Graph webhooks.

  • Sign-in your users with their work or school account to get an access token.
  • Use the access token to create a webhook subscription.
  • Send back a validation token to confirm the notification URL.
  • Listen for notifications from Microsoft Graph.
  • Request for more information in Microsoft Office 365 using data in the notification.

Microsoft Graph Webhook Sample for ASP.NET screenshot

The previous screenshot shows the app's start page. 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 a mail 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:

  • Visual Studio 2015 installed on your development computer.

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

  • The client ID and key from the application that you registered on a Microsoft Azure tenant. Use the Office 365 app registration tool to quickly register an app with the following parameters:

    Parameter Value
    App name <any name>
    App type Web App
    Sign on URL https://localhost:44300/
    Redirect URI https://localhost:44300/
    App permissions Mail.Read

    Copy and store the returned Client ID and Client Secret values. (Learn more about setting up your development environment.)

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. 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 -host-header=localhost:


 ![Example command to run in the ngrok console](readme-images/ngrok1.PNG)

1. 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](readme-images/ngrok2.PNG)

>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](https://github.com/OfficeDev/Microsoft-Graph-Nodejs-Webhooks/wiki/Hosting-the-sample-without-a-tunnel) and [Why do I have to use a tunnel?](https://github.com/OfficeDev/Microsoft-Graph-Nodejs-Webhooks/wiki/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](#ngrok) or a similar tool.

1. Open **GraphWebhooks.sln** in the sample files. 

>You may be prompted to trust certificates for localhost.

1. In Solution Explorer, open the **Web.config** file in the root directory of the project.  
a. For the **ClientId** key, replace *ENTER_YOUR_CLIENT_ID* with the client ID of your registered Azure application.  

b. For the **ClientSecret** key, replace *ENTER_YOUR_SECRET* with the key of your registered Azure application.  

c. 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:

```xml
<add key="ida:NotificationUrl" value="https://0f6fd138.ngrok.io/notification/listen" />
  1. Make sure that the ngrok console is still running, then press F5 to build and run the solution in debug mode.

Use the app

  1. Sign in with your Office 365 work or school account.

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

    App page showing properties of the new subscription

  3. Choose the Watch for notifications button.

  4. Send an email to your Office 365 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

  5. Choose the Delete subscription and sign out button.

Key components of the sample

Controllers

Models

  • Message.cs Represents an Outlook mail message.
  • Notification.cs Represents a change notification.
  • Subscription.cs Represents a webhook subscription. Also defines the SubscriptionViewModel that represents the data displayed in the Subscription view.

Views

Other

Troubleshooting

Issue Resolution
The app opens to a Server Error in '/' Application. The resource cannot be found. browser page. Make sure that a CSHTML view file isn't the active tab when you run the app from Visual Studio.
You're using ngrok and get a Subscription validation request timed out response. Make sure that you used your project's HTTP port for the tunnel (not HTTPS).

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

You can suggest changes for Microsoft Graph on GitHub.

Additional resources

Copyright

Copyright (c) 2016 Microsoft. All rights reserved.

aspnet-webhooks-rest-sample's People

Contributors

ricalo avatar

Watchers

James Cloos avatar Fernando Nogueira avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.