2009-06-22 26 views
6

我在过去做了一些类似以下的事情,在SharePoint中做了很多模仿。您可以在没有提供密码的情况下在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有一个与ServerContext中的SSP,但我不认为这将有助于在所有。

我用反射镜上的KeywordQuery类及其基查询类和它变得很丑陋相当快。我相信确定用户的实际逻辑是在非托管代码中。

那么,我可以这样做吗?

+0

工作链接:http://www.threewill.com/2010/06/connect-to-sharepoint-forwarding-user-identities/ – KjellSj 2012-05-16 17:34:23

回答

1

事实证明,你的ca n在没有密码的情况下在SharePoint中进行模拟搜索。我们在2009年8月发现了这个问题,并且我在更新堆栈溢出问题上一直没有答案。

http://wiki.threewill.com/display/is/2010/06/18/Connect+to+SharePoint+-+Forwarding+User+Identities的细节,特别注意的特殊要求。请注意,这适用于SharePoint 2007和SharePoint 2010.

非常感谢我的同事Eric Bowden完成了所有的工作!

+2

显示链接已损坏? – 2011-03-28 14:46:24

4

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

对于Windows模拟,除非你想冒充使用SPSecurity.RunWithElevatedPrivileges

可以实现Windows模拟如下应用程序池帐户,你会遗憾的是既需要登录名和密码:

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

其中冒领类实现为:

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); 
} 
+1

谢谢,拉尔斯。当然,不是我想听到的答案,但很高兴知道我的选择是什么。我很欣赏Impersonator类。这看起来不错。 – 2009-06-22 12:51:04

+0

好东西拉斯,谢谢你。 – 2009-06-22 21:42:33

相关问题