title: Trying to call SharePoint 2010 List.asmx service. SharePoint 2010 is using Azure AD Application Proxy
body: <p>Our SharePoint 2010 site is now using Azure AD Application Proxy for a single sign-on solution.</p>
  
  <p>We have an Azure Web Job trying to access the SharePoint 2010 List.asmx service.</p>
  
  <p>We added the List.asmx service as a Web Application to Azure AD.</p>
  
  <p>We are able to get a bearer token for the List.asmx service.</p>
  
  <pre class="lang-cs prettyprint-override"><code>var apiClientId = "XXXXXXXX-8ceb-452b-abec-b85a784fbfb5";
  enter code herevar apiAppKey = "XXXXXXXXlx0ZJynQ8mmICeX454VrtOkxa0qqIZuKQY8=";
  var apiResourceId = "https://sharepoint2010baseurl.com/_vti_bin/lists.asmx";
  var tenant = "XXXXXXXX-debb-41a6-9c78-0516c185fa0d";
  var aadInstance = "https://login.windows.net/{0}";
  
  authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
  
  var authContext = new AuthenticationContext(authority);
  if (authContext.TokenCache.ReadItems().Count() &gt; 0)
      authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
  
  var clientCredential = new ClientCredential(apiClientId, apiAppKey);
  var authResult = await authContext.AcquireTokenAsync(apiResourceId, clientCredential);
  
  Assert.IsNotNull(authResult.AccessToken);
  </code></pre>
  
  <p>After adding the bearer token to the RequestMessage Header of the SoapClient call, we get an error.</p>
  
  <pre class="lang-cs prettyprint-override"><code>var soapClient = new ListsSoapClient();
  soapClient.ClientCredentials.UserName.UserName = "BasicUserName";
  soapClient.ClientCredentials.UserName.Password = "BasicPassword";
  
  const string listName = "Announcements";
  
  using (new OperationContextScope(soapClient.InnerChannel))
  {
  
      HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
      requestMessage.Headers["Content-Type"] = "text/xml";
      requestMessage.Headers[System.Net.HttpRequestHeader.Authorization] = "Bearer " + authResult.AccessToken;
      OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
  
      var query = new XElement("Query", "");
      var viewFields = new XElement("ViewFields",
          new XElement("FieldRef", new XAttribute("Name", "ID")),
          new XElement("FieldRef", new XAttribute("Name", "Title")),
          new XElement("FieldRef", new XAttribute("Name", "Body")),      
      );
      var queryOptions = new XElement("QueryOptions", "");
  
      var result = soapClient.GetListItems(listName, null, null, viewFields, null, null, null);
  
      Assert.IsNotNull(result, "Results were null.");
  }
  </code></pre>
  
  <p>The error message we are receiving is as follows:</p>
  
  <blockquote>
    <p>The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Bearer realm="XXXXXXXX-debb-41a6-9c78-0516c185fa0d",authorization_uri="https://login.microsoftonline.com/XXXXXXXX-debb-41a6-9c78-0516c185fa0d/oauth2/authorize",trusted_issuers="00000001-0000-0000-c000-000000000000@*",client_id="XXXXXXXX-f826-46d2-a221-ba9908427c8f"'.</p>
  </blockquote>
  
  <p>The underlying question here is how do you call an asmx service with an Azure AD bearer token?</p>