Skip to content

Instantly share code, notes, and snippets.

@AsishP
Last active June 13, 2019 09:29
Show Gist options
  • Save AsishP/18cde2f8a02a2487004a63ffa661222a to your computer and use it in GitHub Desktop.
Save AsishP/18cde2f8a02a2487004a63ffa661222a to your computer and use it in GitHub Desktop.
Connect to Azure AD in Azure Function
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Collections.Generic;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Extensions.Http;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Extensions.Logging;
string TenantID = <TenantID>;
string authString = "https://login.windows.net/" + TenantID;
string SPServiceUrl = "https://manage.office.com/api/v1.0/" + TenantID + "/activity/feed/subscriptions/content";
string resourceId = "https://manage.office.com";
string clientId = <Client App Id>;
string clientSecret = <Client App secret>;
var authenticationContext = new AuthenticationContext(authString, false);
ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
AuthenticationResult authenticationResult = null;
Task runTask = Task.Run(async () => authenticationResult = await authenticationContext.AcquireTokenAsync(resourceId, clientCred));
runTask.Wait();
string token = authenticationResult.AccessToken;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment