Search code examples
sharepoint-onlinecsommulti-factor-authentication

Sharepoint Online & Azure Tokens


I'm trying to authorise login for a c# app that uses CSOM to query Sharepoint lists and pnp.core to get the client context.

I have an app registration with a client secret. The registration has the permissions Allsites.read & Allsites.write

   string siteUrl ="mysite";
   string clientId = "my app client id";
   string tenantId = "my tenant id";
   string clientSecret = "*******";

    using var clientContext = new AuthenticationManager()
                             .GetACSAppOnlyContext(siteUrl, clientId, clientSecret); 
      clientContext.Load(clientContext.Web);
      clientContext.ExecuteQuery();
      Console.WriteLine(clientContext.Web.Title);

The above code seems to read the key OK (if I change the secret it fails with a key invalid error) but when I try and access the title property of the web I get a 403 Forbidden error.

I'm not sure where I am going wrong here, so any insights would be much appreciated.


Solution

  • AllSites.Read and AllSites.Write permissions are tied with the signed-in user but you connect as a daemon without a signed-in user.

    In that case you need to add application permissions Sites.ReadWrite.All in Azure AD.

    enter image description here