title: Problem authenticating with SharePoint Online when using GetWebLoginClientContext
body: <p>We've got an ASP.NET MVC web application running on Azure on which we want to expose search results from a SharePoint Online tenant. Users in the web app are authenticated on Azure AD, and we want to have them search as themselves (preserving their identity and not use a system account) so that they only see items in the search results that are relevant to them. I thought this would be quite a common requirement.</p>
  <p>Using the <a href="https://www.nuget.org/packages/SharePointPnPCoreOnline/" rel="nofollow noreferrer">SharePointPnPCoreOnline</a> CSOM extentions, we were able to retrieve search results with:</p>
  <pre><code>var authManager = new OfficeDevPnP.Core.AuthenticationManager();
  ClientContext clientContext = authManager.GetWebLoginClientContext(&quot;https://xxxxxx.sharepoint.com&quot;);
  KeywordQuery keywordQuery = new KeywordQuery(clientContext);
  keywordQuery.QueryText = &quot;queryText&quot;;
  SearchExecutor searchExecutor = new SearchExecutor(clientContext);
  ClientResult&lt;ResultTableCollection&gt; results = searchExecutor.ExecuteQuery(keywordQuery);
  clientContext.ExecuteQuery();
  </code></pre>
  <p>(The GetWebLoginClientContext method pops a new window which asks for user credentials if they are not already authenticated. Ideally we would like to have it done silently, but this was the only method wa managed to get working.)</p>
  <p>During dev everything was working OK - we were getting the expected search results. However, once we published the web app to Azure we got the following error when trying to search:</p>
  <blockquote>
  <p>502 - Web server received an invalid response while acting as a gateway or proxy server.
  There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.</p>
  </blockquote>
  <p>One of the suggestions we found for solving http 502 errors was to clear the browser cache, but after we done so it no longer worked on the dev machine either. Instead of displaying a popup requesting user credentials, the popup now just displays the SharePoint Online tenant home page without any login prompt.</p>
  <p>Does anyone have an explanation for this?</p>
  <p>Or if not, a better way to have users search SharePoint Online without the popup displayed by the GetWebLoginClientContext method?</p>