We have angular 13 based application. We are using Azure AD Authentication. When user hit's the Portal URL, he is asked to key in credentials. After successful authentication, user is redirected to a landing page. On top of the page, we need to display the last login date for the logged on user.
I have been looking into https://developer.microsoft.com/en-us/graph/graph-explorer but I could not find anything.
Any pointers would be helpful.
best regards, SP
CodePudding user response:
Check out this link https://docs.microsoft.com/en-us/graph/api/user-list?view=graph-rest-beta&tabs=javascript#example-3-get-users-including-their-last-sign-in-time
If you are connecting to a C# api that does the request to graph :
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var users = await graphClient.Users
.Request()
.Select("displayName,userPrincipalName,signInActivity")
.GetAsync();
HTTP : GET https://graph.microsoft.com/beta/users?$select=displayName,userPrincipalName,signInActivity
JavaScript :
const options = {
authProvider,
};
const client = Client.init(options);
let users = await client.api('/users')
.version('beta')
.select('displayName,userPrincipalName,signInActivity')
.get();
Following is an example of the response :
HTTP/1.1 200 OK
Content-type: application/json
{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#users(displayName,userPrincipalName,signInActivity)",
"value": [
{
"displayName": "Adele Vance",
"userPrincipalName": "[email protected]",
"signInActivity": {
"lastSignInDateTime": "2021-06-17T16:41:33Z",
"lastSignInRequestId": "d4d31c40-4c36-4775-ad59-7d1e6a171f00",
"lastNonInteractiveSignInDateTime": "0001-01-01T00:00:00Z",
"lastNonInteractiveSignInRequestId": ""
}
},
{
"displayName": "Alex Wilber",
"userPrincipalName": "[email protected]",
"signInActivity": {
"lastSignInDateTime": "2021-07-29T15:53:27Z",
"lastSignInRequestId": "f3149ee1-e347-4181-b45b-99a1f82b1c00",
"lastNonInteractiveSignInDateTime": "2021-07-29T17:53:42Z",
"lastNonInteractiveSignInRequestId": "868efa6a-b2e9-40e9-9b1c-0aaea5b50200"
}
}
]
}
CodePudding user response:
The following stackoverflow post answers your question: Getting all users and their last login via graph API
Please note that you need additional API permissions for your Angular App to perform this. The default API permissions will not work.
Are you looking for when the User used his/her MS account to log into your application only? It might be easier store the unique MS id (together with the current DateTime) that you receive from MS Graph API, in your own database / api and use that in your application?