Option Strict On Option Explicit On Option Compare Text Imports System Imports System.ComponentModel Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Xml.Serialization Imports Microsoft.SharePoint Imports Microsoft.SharePoint.Utilities Imports Microsoft.SharePoint.WebPartPages Imports Microsoft.SharePoint.WebControls Imports System.Security.Principal Imports System.Runtime.InteropServices Imports Microsoft.SharePoint.Portal.SingleSignon Public Class IdentityHelper 'Helper functions for SharePoint Identity impersonation Protected Friend Shared Function CreateIdentity(ByVal User As String, _ ByVal Domain As String, ByVal password As String) As WindowsIdentity Dim objToken As New IntPtr(0) Dim ID As WindowsIdentity Const LOGON32_PROVIDER_DEFAULT As Integer = 0 Const LOGON32_LOGON_NETWORK As Integer = 3 'initialize token object objToken = IntPtr.Zero 'attempt to log on Dim blnReturn As Boolean = LogonUser(User, Domain, password, _ LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, objToken) 'check for failure If blnReturn = False Then Dim intCode As Integer = Marshal.GetLastWin32Error() Throw New Exception("Logon failed for user " & User & ": " & intCode.ToString) End If 'return new token ID = New WindowsIdentity(objToken) CloseHandle(objToken) Return ID End Function _ Private Shared Function LogonUser(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 End Function _ Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean End Function End Class