Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
VB.Net: Impersonating an administrator
From Wiki
Warning: The following code is to be used at your own risk.
First you have to create a settings file Called Impersonation.settings
which has 3 fields namely Username, Password and Domain all are of type String and all are application scope and yes I know it is dangerous to keep the password and username in plain text but you should encrypt it yourself that is beyond the scope of this article.
then we have the code.
- Imports System.Runtime.InteropServices
- Imports System.Security.Principal
- Imports System.Security.Permissions
- Namespace Impersonation
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- Public Class Impersonation
- #Region " pInvokes "
- ''' <summary>
- '''
- ''' </summary>
- ''' <param name="lpszUsername"></param>
- ''' <param name="lpszDomain"></param>
- ''' <param name="lpszPassword"></param>
- ''' <param name="dwLogonType"></param>
- ''' <param name="dwLogonProvider"></param>
- ''' <param name="phToken"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- <DllImport("advapi32.dll")> _
- 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 Integer) As Boolean
- End Function
- ''' <summary>
- '''
- ''' </summary>
- ''' <returns></returns>
- ''' <remarks></remarks>
- <DllImport("Kernel32.dll")> _
- Private Shared Function GetLastError() As Integer
- End Function
- #End Region
- #Region " Private members "
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- Private Shared NewContext As WindowsImpersonationContext
- #End Region
- #Region " Enumerables "
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- Private Enum Logon
- NetworkCleartext = 8
- End Enum
- ''' <summary>
- '''
- ''' </summary>
- ''' <remarks></remarks>
- Private Enum Provider
- WindowsNT35 = 1
- WindowsNT40 = 2
- Windows2000 = 3
- End Enum
- #End Region
- #Region " Public methods "
- ''' <summary>
- '''
- ''' </summary>
- ''' <param name="UserName"></param>
- ''' <param name="Domain"></param>
- ''' <param name="Password"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- <SecurityPermission(SecurityAction.Demand, ControlPrincipal:=True, UnmanagedCode:=True)> _
- Private Shared Function GetWindowsIdentity(ByVal UserName As String, ByVal Domain As String, ByVal Password As String) As WindowsIdentity
- Dim SecurityToken As Integer
- Dim Success As Boolean
- Success = LogonUser(UserName, Domain, Password, Logon.NetworkCleartext, Provider.Windows2000, SecurityToken)
- If Not Success Then
- Throw New Exception("Logon Failed. Error: " & GetLastError())
- End If
- GetWindowsIdentity = New WindowsIdentity(New IntPtr(SecurityToken))
- End Function
- ''' <summary>
- '''
- ''' </summary>
- ''' <param name="Impersonate"></param>
- ''' <remarks></remarks>
- Public Shared Sub ImpersonateAdministrator(ByVal Impersonate As Boolean)
- Dim newidentity As WindowsIdentity
- Dim _Settings As New Impersination
- If Impersonate = True Then
- newidentity = GetWindowsIdentity(_Settings.Username, _Settings.Domain, _Settings.Password)
- NewContext = newidentity.Impersonate
- Else
- NewContext.Undo()
- End If
- End Sub
- #End Region
- End Class
- End Namespace



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.