我有一个充当用户前端站点的 .NET Web 应用程序,该应用程序利用单独的 SharePoint 2013 应用程序为用户提供数据。其中一项任务是利用 SharePoint 自己的搜索在前端实现搜索。我通过使用服务器端代码编写搜索查询解决了这个问题。

我遇到的问题是,虽然搜索结果正确返回,但它们没有安全调整。例如,如果我有 2 个文档(文档 A 和文档 B),并且我有一个只能访问文档 A 的用户,则该用户的搜索仍会返回这两个文档。经过一番研究,我认为这可能是由于我创建搜索查询的方式造成的,但到目前为止我还没有找到问题的解决方案。这是创建搜索查询的代码

// Get the result source and the search service proxy
string resultSourceName = ConfigurationManager.AppSettings["ResultSourceName"];

SearchServiceApplicationProxy searchProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(spSite));
FederationManager federationManager = new FederationManager(searchProxy);
SearchObjectOwner searchOwner = new SearchObjectOwner(SearchObjectLevel.Ssa, spSite.RootWeb);
Microsoft.Office.Server.Search.Administration.Query.Source resultSource = federationManager.GetSourceByName(resultSourceName, searchOwner);

// Create a query object, set its result source and set the properties to return in the search results table
KeywordQuery query = new KeywordQuery(spSite);
query.SourceId = resultSource.Id;

我有两个问题:

  1. 导致此安全调整问题的上述代码有什么问题?
  2. 如果不是代码问题,导致此问题的其他可能原因是什么?
有帮助吗?

解决方案 2

我终于找到了解决问题的方法。感谢 Mike Lutge 为我指明了正确的方向。

有趣的是,虽然 spSite.UserTokenspSite.RootWeb.CurrentUser 两者都引用执行搜索查询的用户,搜索似乎仍然使用系统帐户上下文运行。我相信无论是 KeywordQuery 或者 SearchExecutor 类忽略用户 spSite 并作为系统帐户自动运行它。为了强制它模拟用户,我使用以下代码创建 spSite:

System.Security.Principal.WindowsIdentity w = ((System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity);

using (WindowsImpersonationContext ctx = w.Impersonate())
using (SPSite spSite = new SPSite(ConfigurationManager.AppSettings["TheSPSite"]))
{
    response = searchRequest.searchFunction(searchRequest, spSite);
}

这允许我运行搜索查询并对其进行正确的安全调整。

其他提示

如果您上面发布的代码在SharePoint 2013应用程序中运行,则按顺序排列为您的搜索查询作为用户执行,您需要将以下内容添加到AppManifest.xml:

<AppPermissionRequests>
    <AppPermissionRequest Scope="http://sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal" />
</AppPermissionRequests>
.

许可以下: CC-BY-SA归因
scroll top