question

MarcSchwarz-9828 avatar image
0 Votes"
MarcSchwarz-9828 asked ·

Check if logged in to Sharepoint

On an external site I need to know if a user is logged in in Sharepoint. Is this possible, and if yes, how? I tried to access via API:

 var token;
     $(document).ready(function () {
         requestToken();
     });
    
     function requestToken() {
         $.ajax({
             "async": true,
             "crossDomain": true,
             "url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token",
             "method": "POST",
             "headers": {
                 "content-type": "application/x-www-form-urlencoded"
             },
             "data": {
                 "grant_type": "client_credentials",
                 "client_id ": "{client_id}",
                 "client_secret": "{client_secret}",
                 "scope ": "https://graph.microsoft.com/.default"
             },
             success: function (response) {
                 console.log(response);
                 token = response.access_token;
                 getUserInformation();
             },
             error: function (error) {
                 console.log(JSON.stringify(error));
             }
         })
     }
    
     function getUserInformation() {
         $.ajax({
             method: 'GET',
             url: "https://graph.microsoft.com/v1.0/me",
             headers: {
                 'Authorization': 'Bearer ' + token,
                 'Content-Type': 'application/json'
             },
         }).success(function(response) {
             console.log(response);
         }).error(function(error) {});
     }

However I get a 404 error: Resource '{id}' does not exist or one of its queried reference-property objects are not present.

I think I cant do this because I am not making the request with an user. I can access https://graph.microsoft.com/v1.0/users, but I would need to know the id of the logged in user. Please help, dont know what else to try...

azure-active-directory
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MarileeTurscak avatar image
0 Votes"
MarileeTurscak answered ·

Usually that error is related to an issue with the request. Have you checked to make sure there isn't an extra space or something in the URL that could cause this? Can you include the full response for me? https://github.com/microsoftgraph/microsoft-graph-docs/issues/3539

Are you able to see the logs in the portal?

One way to check is in the Security and Compliance Center under "View Reports." There you can view Sharepoint audit logs. https://docs.microsoft.com/en-us/microsoft-365/compliance/reports-in-security-and-compliance?view=o365-worldwide

You can also check the Azure AD sign-in and activity reports. https://docs.microsoft.com/en-us/microsoft-365/compliance/reports-in-security-and-compliance?view=o365-worldwide

In Office 365, we can use the default Reports in Admin Center (Office 365 Reports in the Admin Center - SharePoint activity).Or we can also use PowerShell to export all Office 365 users' last login dates to a CSV file.

In SharePoint on-premise, we can also use PowerShell with server code to achieve it.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.