Bridging the Gap: Calling Azure Functions from SPFx Cross Tenants
In today's interconnected digital world, integrating cloud services is essential. However, complex environments like multi-tenant Azure setups can pose significant challenges. This blog addresses the hurdles we faced when connecting an SPFx web part to an Azure Function across different tenants.
We'll cover:
By the end of this post, you'll have a clear understanding of how to overcome these challenges and successfully integrate your Azure Function with an SPFx web part across tenants.
The Client's Need
Our client uses a complex Azure setup with two tenants. One houses their Azure Function, while the other hosts SharePoint Online. We aimed to create an SPFx web part that seamlessly interacts with the function for enhanced functionalities, but secure communication across tenants was crucial
The Problem we encountered
Our client operates in a complex cloud environment with two distinct Azure tenants. One tenant house essential Azure resource (Function app), while the other hosts their SharePoint Online instance. Our objective was to build a solution where SPFx webpart could seamlessly interact with an Azure Function to complete its task, and it was important that the web part and function could securely communicate with each other using user context.
While exploring Microsoft's documentation, Referenced a guide on building a multi-tenant enterprise API with Azure AD and connecting it with SharePoint Framework (link:https://learn.microsoft.com/en-us/sharepoint/dev/spfx/use-aadhttpclient-enterpriseapi-multitenant). However, it appeared the guide's configuration might not fully align with the current Azure portal interface.
We opted for the multi-tenant application approach to avoid storing sensitive information, like secrets, directly within the SPFx code, which would be required for generating an application token to access the Azure Function.
We meticulously followed the guidelines outlined in the Microsoft document, but unfortunately, we encountered a 403 error - Forbidden when attempting to invoke the Azure Function from SPFx web part using the user context generated token.
The Solution
Prerequsites
Creating a multi-tenant application
领英推荐
Configuring the Azure Function for authentication
Why we require SharePoint Online Client Extensibility Web Application Principal Client Id ?
Developers building a SharePoint Framework solution that requires access to specific resources secured with Azure AD list these resources along with the required permission scopes in the solution manifest.
When deploying the solution package to the app catalog, SharePoint creates permission requests and prompts the administrator to manage the requested permissions.
For each requested permission, a global or SharePoint administrator can decide whether they want to grant or deny the specific permission.
When the administrator grants a specific permission, its added to the SharePoint Online Client Extensibility Azure AD application, which is provisioned by Microsoft in every Azure AD and which is used by the SharePoint Framework in the OAuth flow to provide solutions with valid access tokens.
All permissions granted through web API requests are stored with the SharePoint Online Client Extensibility Azure AD application
Call Azure function from SPFx code
"webApiPermissionRequests": [
{
"resource": "<multitenant-application-name>",
"scope": "user_impersonation",
"appId": "<Application Id>",
"replyUrl": "https://<function app name>/.auth/login/aad/callback"
}
]
this.context.aadHttpClientFactory.getClient('<Client ID of multitenant application>').then((client: AadHttpClient): void => {
const requestOptions: IHttpClientOptions = {
body: JSON.stringify({
any value which accepts by your function
})
};
client.post('https://<function app name>/api/getUserPreferences', AadHttpClient.configurations.v1, requestOptions)
.then((response: any) => {
return response.json();
}).then((res: any) => {
console.log(res);
})
.catch((e: Error) => {
console.log(e);
})
}).catch((err: Error) => {
debugger;
console.log(err);
})
Conclusion
In this guide, we addressed the challenges of setting up a multi-tenant application. We outlined the process for creating the application and configuring Azure Function authentication. We clarified the necessity of the SharePoint Online Client Extensibility Web Application Principal for integration.
Finally, we demonstrated how to call the Azure Function from SPFx code. This setup ensures secure and efficient interaction between SharePoint and Azure.
Evolving with the Tech Tide | Vision. Clarity. Ingenuity. Audacity.
5 个月Excellent article! It's refreshing to see such interesting insights and solution presented with clarity and depth. Looking forward to more articles like this ??
Infra Technology Specialist for Modern Workplace at Cognizant | Azure | Exchange | Intune | SharePoint | MS Teams | M365 Security | PowerBi | M365 Applications
7 个月Insightful!