Many companies are using Microsoft 365 (former Office 365). Besides providing access to Office applications, Microsoft provides an Azure-based Active Directory, allowing users to log in to access services such as Microsoft Sharepoint or OneDrive using a Web Browser.
All Microsoft services use the same Single Sign-On, based on the email address. From 4D v19 R6, 4D NetKit allows you to join this concept, allowing users to log in to your application using their usual company credentials.
Besides allowing Single Sign-On, it will enable to retrieve information for the authenticated user such as email, department, and phone number. If permitted by the administrator, you can even retrieve such details on colleagues from the same company.
Let’s see how it works.
First, to access a user’s information, you’ll need to get access on their behalf and use the New OAuth2 provider command to create your OAuth2 provider:
$param:=New object()
$param.name:="Microsoft"
$param.permission:="signedIn"
$param.clientId:="XXX"
$param.redirectURI:="http://127.0.0.1:50993/authorize/"
$param.scope:="https://graph.microsoft.com/.default"
// Create new OAuth2 object
$tokenProvider:=New OAuth2 provider($param)
When your OAuth2 provider is ready, use the New Office365 provider command to instantiate an object that will manage the access to your Microsoft 365 account through the Microsoft Graph API:
var $Office365 : cs.NetKit.Office365Provider
$Office365:=New Office365 provider($tokenProvider)
user information
You can get the user information like displayName, givenName, and more.
If you are connected in Sign In mode, you can get the current user information:
$currentUser:=$Office365.user.getCurrent()
//$currentUser=
//{displayName:My Name,givenName:Name,mail:myname@mycompany.com,...}
If you belong to Azure AD “work or school” account, you can access the information of a specific user of your organization:
$user:=$Office365.user.get("myname@mycompany.com")
or access the list of all the users of your organization:
$users:=$Office365.user.list()
Check out the documentation for more details, and don’t hesitate to share with us your experience on the 4D forum.