WSS 2.0 - 使用用户令牌创建SPSite

时间:2009-02-13 07:10:58

标签: sharepoint

我正在开发一个应用程序,使用对象模型将文档上传到sharepoint文档库。我的应用程序在LocalSystem用户下作为Windows服务运行。在这里,在上传时我需要将上下文设置为另一个用户。我有我的用户名。在MOSS 2007中,我可以选择在初始化SPSite对象时指定用户令牌。我正在寻找类似的WSS 2.0的东西,但找不到一个。

如何设置WSS 2.0的用户上下文?请帮忙。

谢谢, Jagannath

2 个答案:

答案 0 :(得分:2)

此功能最初是在WSS 3.0对象模型中引入的。 WSS 2.0中没有类似的功能。

答案 1 :(得分:1)

编辑: 肯定会有一种更简单的方法,但这是一个很好的可重用类,我只是广泛使用它来进行这种类型的操作(但是不是SP OM,至少还没有。)

您必须编写一份单独的.Net代码(一些非管理调用)来执行您的用户模拟,然后在模拟该用户时调用SP对象模型。然后,您可以恢复用户帐户:

VB样本

Public Class UserImpersonation

    Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As [String], _
        ByVal lpszDomain As [String], ByVal lpszPassword As [String], _
        ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
        ByRef phToken As IntPtr) As Boolean

    <DllImport("kernel32.dll")> _
    Private Shared Function FormatMessage(ByVal dwFlags As Integer, ByRef lpSource As IntPtr, _
        ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef lpBuffer As [String], _
        ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer

    End Function

    Private Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean

    Private Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As IntPtr, _
            ByVal SECURITY_IMPERSONATION_LEVEL As Integer, _
            ByRef DuplicateTokenHandle As IntPtr) As Boolean

    <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
    Public Shared Function ImpersonateUser(ByVal strDomain As String, ByVal strUserid As String, ByVal strPassword As String) As WindowsImpersonationContext

        Dim tokenHandle As New IntPtr(0)
        Dim dupeTokenHandle As New IntPtr(0)

        Try
            ' Get the user token for the specified user, domain, and password using the 
            ' unmanaged LogonUser method.  
            ' The local machine name can be used for the domain name to impersonate a user on this machine.

            Const LOGON32_PROVIDER_DEFAULT As Integer = 0
            'This parameter causes LogonUser to create a primary token.
            Const LOGON32_LOGON_INTERACTIVE As Integer = 2

            tokenHandle = IntPtr.Zero

            ' Call LogonUser to obtain a handle to an access token.
            Dim returnValue As Boolean = LogonUser(strUserid, strDomain, strPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle)

            If returnValue = False Then
                Dim ret As Integer = Marshal.GetLastWin32Error()
                Throw New System.ComponentModel.Win32Exception(ret)
            Else
                ' Use the token handle returned by LogonUser.
                Dim newId As New WindowsIdentity(tokenHandle)
                Dim ImpersonatedUser As WindowsImpersonationContext = newId.Impersonate()

                Return ImpersonatedUser
            End If

        Catch ex As Exception
            Console.WriteLine("UserImpersonation.impersonateUser Exception Occurred: " + ex.Message)

            Return Nothing
        End Try

        ' Free the tokens.
        If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
            CloseHandle(tokenHandle)
        End If
    End Function


    Public Shared Function UndoImpersonate(ByVal WIC As WindowsImpersonationContext) As Boolean
        Try
            ' Stop impersonating the user.
            WIC.Undo()

            Return True
        Catch ex As Exception
            Console.WriteLine(("Exception occurred. " + ex.Message))

            Return False
        End Try

    End Function
End Class