Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Using Graph Bindings With Node Js Azure Function Apps

- 24 Jan 2020

Using Graph Bindings with Node.JS Azure

Draft

  1. Create a function app and enable AppServices Authentication for AAD but don’t require login.  Use the express settings.  Since we are using the app identity ensure you Allow Anonymous requests.

clip_image002

  1. Navigate to the root of the site and Admin Consent access, make sure you can log in.
  2. Only if you want to develop local Next retrieve the following variables from the KUDU console from the Environment section of the Kudu interface:
  3. </ol>

    clip_image004

    And set this in the local.settings.json

    “WEBSITE_AUTH_CLIENT_ID”:  “fbd9bb9c-xxxxxx-245221e766cc”,

    “WEBSITE_AUTH_CLIENT_SECRET”:”ctboPyySE/JD2oGlAlXxxxx7ylVNQRhXU=”,

    “WEBSITE_AUTH_OPENID_ISSUER”: “https://sts.windows.net/fb840fe1-3b88xxxx-18fbf56fae21/“,

    “BYOB_TokenMap”:”C:\temp”

    1. Create the extensions.csproj file in the wwwroot folder with these values.  You can use the app service editor to do this (https://yourfuncsite.scm.azurewebsites.net/dev

    </p> <p><Project Sdk="Microsoft.NET.Sdk"></p> <p>  <PropertyGroup></p> <p>    <TargetFramework>netcoreapp2.2</TargetFramework></p> <p>    <WarningsAsErrors /></p> <p>  </PropertyGroup></p> <p>  <ItemGroup></p> <p>    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.AuthTokens" Version="1.0.0-beta6" /></p> <p>    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" /></p> <p>    <PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.1.2" /></p> <p>  </ItemGroup></p> <p>

    </Project>

    1. Stop the website
    2. Clean your Host.json so it ONLY has this:
    3. </ol>

      {

        “version”: “2.0”

      }

      1. Go to WWWROOT folder in the kudu console and run this command:

      D:\home\site\wwwroot>

      dotnet build extensions.csproj -o bin --no-incremental --packages D:\home\.nuget

      Wait until the command prompt comes back and restart the site.

      1. Create an httptrigger function an use this function.json

      {

        “bindings”: [

          {

            “name”: “req”,

            “type”: “httpTrigger”,

            “direction”: “in”

          },

          {

            “type”: “token”,

            “direction”: “in”,

            “name”: “graphToken”,

            “resource”: “https://graph.microsoft.com“,

            “identity”: “clientCredentials”

          },

          {

            “name”: “$return”,

            “type”: “http”,

            “direction”: “out”

          }

        ],

        “disabled”: false

      }

      And index js for the code to prove you get the token!

      module.exports = async function (context, req) {

          context.log(‘JavaScript HTTP trigger function processed a request.’);

           let token = “Bearer ” + context.bindings.graphToken;

          context.log(token);

              context.res = {

                  // status: 200, /* Defaults to 200 */

                  body: “Hello ” + token

              };

      };

      You will see it dumps the current bearer token based on the ID of the function app.

      References:  https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-microsoft-graph

      https://github.com/Azure/azure-functions-microsoftgraph-extension/blob/master/samples/GraphExtensionSamples/WebhookTriggerExamples.cs

      https://github.com/Azure/azure-functions-microsoftgraph-extension

<< Go Back