Overview
Azure functions are helpful to perform processing outside of SharePoint. In the previous article,
SharePoint Framework - Call Azure Function, we explored an option to create Azure functions with anonymous access. In real scenarios, it is not recommended to have Azure functions with anonymous access.
In this article, we will explore how to secure Azure function with Azure AD.
Azure AD App Registration
- Open Azure Portal https://portal.azure.com
- From left menu, click “Azure Active Directory”
- Click “App registrations”
- Click “New application registration”
- Specify any name for app registration
- Select “Application type” as “Web app / API”
- Specify “Sign-on URL” as url of the Azure function we will be creating in next step
- Click Create
Get the App ID URI
- Click “Settings”
- Click “Properties”
- Note down “App ID URI” for future reference
App registration permissions
By default, app registration have “Sign in and read user profile” permission. To modify the permissions, follow below steps,
- Click Settings
- Click “Required permissions” under “API ACCESS”
- Click “Add”
- Add or update permissions as needed.
Grant permissions to App registration
As an administrator, grant permissions to the App registration for all the users.
- Click Settings
- Click “Required permissions” under “API ACCESS”
- Click “Grant permissions”
- Click “Yes”
Azure Function
Azure functions are serverless computing. It is an event driven, compute on demand experience. Follow the below steps to create an Azure function,
- Open Azure Portal https://portal.azure.com
- Click Create Resource
- Under Compute, select Function App
- Fill in the information to create the Function App
- Click Create
- Once the Azure function is ready, click “Platform features” tab
- Under Networking, click “Authentication / Authorization”
- In the option “App Service Authentication”, select “ON”
- For "Action to take when request is not authenticated" option, select “Log in with Azure Active Directory”
- Under “Authentication Providers”, select “Azure Active Directory”
- Select “Management mode” as “Advanced”
- In the “Client ID” textbox, paste the Application ID we created in earlier step - “Azure AD App Registration”
- In “Allowed Token Audiences”, copy App ID URI we created in earlier step
- Click OK
Enable CORS on Azure Function
The Azure functions are hosted in MS Azure and they run in a different domain than our SharePoint site where our SharePoint Framework (SPFx) web part is hosted. By default cross domain calls are not allowed from SharePoint. To overcome this we will have to enable CORS (Cross-Origin Resource Sharing) in Azure function.
Follow the below steps to enable CORS on Azure function,
- Click Platform features
- Under API, click CORS
- Specify the Office 365 tenant domain url and SharePoint local workbench url
- Click Save
Implement Azure Function
- Open Visual Studio (2015 or 2017)
- If you are using Visual Studio 2015, install “Visual Studio Tools for Azure Functions” from here
- Click “New Project”
- Under Visual C#, select Cloud > Azure Functions
- Name the project (e.g. SecureFunctionApp)
- Click OK
- Right click the project name. Select Add > New Item…
- Add Azure function named "UserInformation"
- Click Add
- Select Http trigger
- For “Access rights” select “Anonymous”. We are using Azure AD in the Function app to secure it.
- Click OK
- Use the below code in Function app
- using Microsoft.Azure.WebJobs;
- using Microsoft.Azure.WebJobs.Extensions.Http;
- using Microsoft.Azure.WebJobs.Host;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Formatting;
- using System.Security.Claims;
- using System.Threading.Tasks;
-
- namespace SecureFunctionApp
- {
- public static class UserInformation
- {
- [FunctionName("UserInformation")]
- public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
- {
- log.Info("C# HTTP trigger function processed a request.");
-
- var result = new Dictionary<string, string>();
-
-
- foreach (Claim claim in ClaimsPrincipal.Current.Claims)
- {
- result.Add(claim.Type, claim.Value);
- }
-
- return req.CreateResponse(HttpStatusCode.OK, result, JsonMediaTypeFormatter.DefaultMediaType);
- }
- }
- }
- Right click the project name. Select Publish…
- Click “Select existing”
- Click Publish
- Select the earlier created Function app
- Click OK to publish the function to the Azure function app.
Summary
In the production environment, it is always recommended to secure the Azure function. Azure functions can be easily secured with Azure AD by associating it with Azure AD App Registration.