How to create a sharepoint current user context using

Anmol Chourasia 0 Reputation points
2024-11-29T08:08:23.1666667+00:00

User's image

I am trying to create the current user's SharePoint context using the following details:

  • Client ID
  • Tenant ID
  • Certificate

I am using UseOpenIdConnectAuthentication and ClaimsPrincipal for this process. Specifically, I want to understand if it's possible to use these components to create the current user's SharePoint context.

Microsoft 365 and Office SharePoint Development
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Emily Du-MSFT 51,836 Reputation points Microsoft External Staff
    2024-12-02T09:02:42.45+00:00

    What is the version of your SharePoint? SharePoint Online or SharePoint Server Subscription Edition?

    If you are using SharePoint Online, as this authentication method is only supported in SharePoint Server Subscription Edition, it is not possible.

    If you are using SharePoint Server Subscription Edition, first you need to obtain the claims from the OIDC token after authenticating the user, then create the ClientContext in the SharePoint Server Subscription Edition.

    Here is a simplified example of how you might set this up in code:

    ClientContext clientContext = new ClientContext(<sharepointurl>);
    clientContext.AuthenticationMode = ClientAuthenticationMode.Anonymous;
    clientContext.ExecutingWebRequest += (sender, e) =>
    {
        e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + accessToken; // Use your access token
    };
    

    Reference:

    https://learn.microsoft.com/en-us/sharepoint/security-for-sharepoint-server/oidc-1-0-authentication


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Anmol Chourasia 0 Reputation points
    2024-12-10T06:25:40.9+00:00

    @Emily Du-MSFT
    Thank you for your reply. We are using SharePoint Online and have some restrictions as the system was developed in .NET Framework 4.5, making certain latest code implementations inapplicable. Is there a workaround?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.