2012-12-23 27 views
1

我试图解决许多文档存储在不同的SharePoint站点(不一定在同一台服务器上,这意味着它们不是同一站点的子站点)的问题。是否有可能从网站获得结果而无法访问?

我正在考虑创建某种自定义SharePoint搜索,但我的问题是这样的 - 是否有可能无法访问特定SharePoint站点的用户从该站点获取搜索结果?

这意味着如果他们搜索“文档X”,并且该文档位于网站上,则他们没有权限,我希望他们在搜索结果页面上看到该文档位于那里,但仍然没有让他们在没有获得许可的情况下访问它(只是看它是否存在)。

非常感谢。

回答

0

下面是如何模拟特定用户并将用户看到的搜索结果返回给所有用户的示例。不用说,这当然是一个安全风险,所以谨慎使用:

using System; 
using System.ComponentModel; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.WebControls; 
using System.Data; 
using System.Security.Principal; 

using Microsoft.Office.Server.Search.Administration; 
using Microsoft.Office.Server.Search.Query; 
using Microsoft.SharePoint.Administration; 

namespace SharePointSearchImpersonation.WebPart1 
{ 
    [ToolboxItemAttribute(false)] 
    public class QuerySearchImpersonatedWebPart : WebPart 
    { 
     protected override void CreateChildControls() 
     { 
      try 
      { 
       // Run elevated since the user, the apppool account, that impersonate other users needs the following 
       // local policies (run secpol.msc): "Impersonate a client after authentication" & "Act as part of the operating system" 
       // The apppool account must be granted above rights. 
       SPSecurity.RunWithElevatedPrivileges(
        delegate 
         { 
          // Setup the windows identity to impersonate the search as. 
          WindowsIdentity identity = new WindowsIdentity("[email protected]"); 
          // Create a new ImpersonationContext for the identity 
          using (WindowsImpersonationContext wic = identity.Impersonate()) 
          { 
           SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>(); 
           // Get the search proxy by service application name 
           SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("Search Service Application"); 
           KeywordQuery query = new KeywordQuery(searchProxy); 
           query.ResultTypes = ResultType.RelevantResults; 
           query.QueryText = "test"; 
           query.RowLimit = 25; 
           ResultTableCollection result = query.Execute(); 
           DataTable dt = new DataTable(); 
           dt.Load(result[ResultType.RelevantResults]); 

           GridView gv = new GridView(); 
           Controls.Add(gv); 
           gv.AutoGenerateColumns = true; 
           gv.DataSource = dt; 
           gv.DataBind(); 

          } 
         }); 
      } 
      catch (Exception ex) 
      { 
       Controls.Add(new LiteralControl() {Text = ex.ToString()}); 
      } 
     } 
    } 
}