title: C# CSOM Sharepoint Bearer request from azure active directory
body: <p>I am using the following approach as the basis of this (<a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-webapi-dotnet" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-webapi-dotnet</a>). </p>
  
  <p>I got all this example working after setting up azure. But now we need to port it to an actual existing mobile app and web api app. The mobile app can get the <strong>Bearer</strong> token, but when we pass it to the web api, we pass this in a <strong>CSOM</strong> request as follows, but we still get a <strong>401 Unauthroised</strong> response.</p>
  
  <pre><code>public static ClientContext GetSharepointBearerClientContext(this JwtTokenDetails tokenDetails)
      {
          var context = new ClientContext(tokenDetails.SiteUrl);
          //context.AuthenticationMode = ClientAuthenticationMode.Anonymous;
          context.ExecutingWebRequest += new EventHandler&lt;WebRequestEventArgs&gt;((s, e) =&gt;
          {
              e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + tokenDetails.BearerToken;
          });
          return context;
      }
  </code></pre>
  
  <p>Our web api doesn't use any of the tech as in the example above, as I presume that we should just be able to pass the token through the CSOM request in the header, but this is not working, what else could I look at?</p>
  
  <p>I have assigned the <strong>Office 365 Sharepoint Online (Microsoft.Sharepoint)</strong> permission and set the following</p>
  
  <p><a href="https://i.stack.imgur.com/7dYDF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7dYDF.png" alt="Required Permissions"></a></p>
  
  <p>I have also done the same for the app registration, which we don't really use! Still not sure how the app registration comes into it)...</p>