asp.net-mvc - 使用 GetWebLoginClientContext 时出现 SharePoint Online 身份验证问题

标签 asp.net-mvc azure sharepoint-online csom

我们有一个在 Azure 上运行的 ASP.NET MVC Web 应用程序,我们希望在其中公开来自 SharePoint Online 租户的搜索结果。 Web 应用程序中的用户在 Azure AD 上进行身份验证,我们希望他们以自己的身份进行搜索(保留其身份而不使用系统帐户),以便他们只能看到搜索结果中与他们相关的项目。我认为这是一个很常见的要求。

使用SharePointPnPCoreOnline CSOM 扩展,我们能够通过以下方式检索搜索结果:

var authManager = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext clientContext = authManager.GetWebLoginClientContext("https://xxxxxx.sharepoint.com");
KeywordQuery keywordQuery = new KeywordQuery(clientContext);
keywordQuery.QueryText = "queryText";
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();

(GetWebLoginClientContext 方法会弹出一个新窗口,如果用户尚未经过身份验证,则要求提供用户凭据。理想情况下,我们希望以静默方式完成此操作,但这是我们设法开始工作的唯一方法。)

在开发期间一切正常 - 我们得到了预期的搜索结果。但是,当我们将 Web 应用程序发布到 Azure 后,我们在尝试搜索时收到以下错误:

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.

我们发现解决 http 502 错误的建议之一是清除浏览器缓存,但在我们这样做之后,它也不再适用于开发计算机。现在,弹出窗口不再显示请求用户凭据的弹出窗口,而是仅显示 SharePoint Online 租户主页,而没有任何登录提示。

有人对此有解释吗?

或者,如果没有,是否有更好的方法让用户搜索 SharePoint Online,而不显示 GetWebLoginClientContext 方法显示的弹出窗口?

最佳答案

最终不是使用 GetWebLoginClientContext 方法(弹出一个窗口供用户输入凭据)解决此问题,而是使用 GetAzureADWebApplicationAuthenticatedContext 方法(使用 oauth 进行静默身份验证)访问 token 。问题在于获取正确的 token 来传递给该方法,我们通过使用 MicrosoftGraphHelper.GetAccessTokenForCurrentUser(url) 成功地做到了这一点,发现 here

关于asp.net-mvc - 使用 GetWebLoginClientContext 时出现 SharePoint Online 身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55347861/

相关文章:

javascript - 获取隐藏的 igGrid 列的单元格值

azure - 无法将 CNAME 域添加到域名中包含数字的 Azure 网站

azure - 如何使用 API 或 CLI 获取 Azure Devops 项目中的用户列表

office365 - 如何在两个 o365 租户之间建立信任

google-analytics - Google Analytics(分析)和Sharepoint Online

asp.net-mvc - 仅将 MiniProfiler 用于调试或本地请求

asp.net - Cckfinder azure 以编程方式设置后端

asp.net - 在 View 中使用 RenderPartial 时不显示 ValidationSummary()

azure - 如何查找谁在Azure Portal的Keyvault中创建/编辑了 secret ?

css - 内容编辑器 Web 部件覆盖 Sharepoint CSS