Check if logged in to Sharepoint

Marc Schwarz 1 Reputation point
2020-04-17T13:00:59.237+00:00

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...

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,422 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marilee Turscak-MSFT 33,801 Reputation points Microsoft Employee
    2020-04-21T22:10:24.39+00:00

    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://learn.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://learn.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.

    0 comments No comments