sharepoint - 您能否在不提供密码的情况下在 SharePoint 中执行模拟搜索?

标签 sharepoint search impersonation

过去,我通过执行以下操作在 SharePoint 中进行了大量模拟。

SPWeb web = SPContext.Current.Web;
string currentWebUrl = web.Url;
SPUser user = web.EnsureUser(loginToImpersonate);
using (SPSite site = new SPSite(currentWebUrl, user.UserToken)
{
    using (SPWeb impersonatedWeb = site.OpenWeb())
    {
        // Any SharePoint access here to 'impersonatedWeb'
        // is impersonated as 'loginToImpersonate'
    }
}

请注意,这不需要您模拟的用户的密码,但需要特定的代码访问安全性才能运行。附带说明一下,EnsureUser 调用还要求当前用户是管理员,但还有其他方法可以用来代替 EnsureUser 来获取 SPUser 对象(尝试使我的代码片段对于这个问题保持简单)。

现在我已经做好了准备……我现在想要针对 MOSS 或 WSS 查询引擎执行 FullTextSQLQuery 或 KeywordQuery,并根据模拟用户获得安全修整的结果。两个对象都可以在构造函数上使用 SPSite,但忽略我的模拟逻辑。它们与当前登录的用户一起使用(HTTPContext.Current.User)。

还有其他构造函数:应用程序名称(字符串),对于 MOSS,有一个带有 SSP 的 ServerContext,但我认为这些根本没有帮助。

我在 KeywordQuery 类及其基础 Query 类上使用了 Reflector,它很快变得非常难看。我相信确定用户的实际逻辑在非托管代码中。

那么,我可以这样做吗?

最佳答案

您需要真正的 Windows 模拟来执行此操作。 SPSite 模拟不是真正的模拟 - 它只是告诉 WSS 对象模型将另一个用户 ID 写入内容数据库中创建和修改的字段。

不幸的是,对于 Windows 模拟,您将需要登录名和密码,除非您想使用 SPSecurity.RunWithElevatedPrivileges 模拟应用程序池帐户。

您可以按如下方式实现 Windows 模拟:

using (Impersonator imp = new Impersonator("user", "domain", "password"))
{
  // Do stuff impersonated
}

其中 Impersonator 类实现为:
public sealed class Impersonator : IDisposable
{
  private WindowsImpersonationContext impersonationContext;

  public Impersonator(string user, string domain, string password)
  {
    WindowsIdentity id = Logon(user, domain, password);
    impersonationContext = id.Impersonate();
  }

  public void Dispose()
  {
    if (impersonationContext != null)
    {
      impersonationContext.Undo();
      impersonationContext = null;
    }
  }

  private WindowsIdentity Logon(string user, string domain, string password)
  {
    WindowsIdentity identity;
    IntPtr handle = IntPtr.Zero;
    bool logonSucceeded = LogonUser(
      user, domain, password,
      8,   // LOGON32_LOGON_NETWORK_CLEARTEXT
      0,   // LOGON32_PROVIDER_DEFAULT
      ref handle);

    if (!logonSucceeded)
    {
      int errorCode = Marshal.GetLastWin32Error();
      throw new UnauthorizedAccessException("User logon failed. Error Number: " + errorCode);
    }

    identity = new WindowsIdentity(handle);
    CloseHandle(handle);

    return identity;
  }

  [DllImport("advapi32.dll", SetLastError = true)]
  private static extern bool LogonUser(string lpszUsername,
    string lpszDomain,
    string lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    ref IntPtr phToken);

  [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  private static extern bool CloseHandle(IntPtr handle);
}

关于sharepoint - 您能否在不提供密码的情况下在 SharePoint 中执行模拟搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1025409/

相关文章:

sharepoint - 当用户在 AD 中不存在时,如何在 SharePoint 中创建新用户?

internet-explorer - 在 IE 中嵌入框小部件 (IFrame) 不起作用,但在 Chrome 中有效

javascript - 在 Sharepoint 2013 中使用 JavaScript 检索查找的链接列表

sharepoint - 可以在另一个内部加载 Web 部件吗?

javascript - 如何匹配引号内的搜索词

c# - WCF Web 服务 - 同一台服务器上的多跃点模拟

search - 如何使用 VIM 在我的项目的所有文件中搜索?

mongodb - mongodb 使用 mongoose 进行文本搜索

c# - 读取 SharePoint 2010 列表时出现“项目不存在”错误

c++ - 使用 CreateProcessAsUser 将焦点放在从系统服务启动的窗口上