Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

VB.Net: Impersonating an administrator

From Wiki

Jump to: navigation, search

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.

  1. Imports System.Runtime.InteropServices
  2. Imports System.Security.Principal
  3. Imports System.Security.Permissions
  4.  
  5. Namespace Impersonation
  6.     ''' <summary>
  7.     '''
  8.     ''' </summary>
  9.     ''' <remarks></remarks>
  10.     Public Class Impersonation
  11.  
  12. #Region " pInvokes "
  13.         ''' <summary>
  14.         '''
  15.         ''' </summary>
  16.         ''' <param name="lpszUsername"></param>
  17.         ''' <param name="lpszDomain"></param>
  18.         ''' <param name="lpszPassword"></param>
  19.         ''' <param name="dwLogonType"></param>
  20.         ''' <param name="dwLogonProvider"></param>
  21.         ''' <param name="phToken"></param>
  22.         ''' <returns></returns>
  23.         ''' <remarks></remarks>
  24.         <DllImport("advapi32.dll")> _
  25.      Private Shared Function LogonUser(ByVal lpszUsername As String, _
  26.             ByVal lpszDomain As String, ByVal lpszPassword As String, _
  27.             ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
  28.      ByRef phToken As Integer) As Boolean
  29.         End Function
  30.  
  31.         ''' <summary>
  32.         '''
  33.         ''' </summary>
  34.         ''' <returns></returns>
  35.         ''' <remarks></remarks>
  36.         <DllImport("Kernel32.dll")> _
  37.     Private Shared Function GetLastError() As Integer
  38.         End Function
  39. #End Region
  40.  
  41. #Region " Private members "
  42.         ''' <summary>
  43.         '''
  44.         ''' </summary>
  45.         ''' <remarks></remarks>
  46.         Private Shared NewContext As WindowsImpersonationContext
  47. #End Region
  48.  
  49. #Region " Enumerables "
  50.         ''' <summary>
  51.         '''
  52.         ''' </summary>
  53.         ''' <remarks></remarks>
  54.         Private Enum Logon
  55.             NetworkCleartext = 8
  56.         End Enum
  57.  
  58.         ''' <summary>
  59.         '''
  60.         ''' </summary>
  61.         ''' <remarks></remarks>
  62.         Private Enum Provider
  63.             WindowsNT35 = 1
  64.             WindowsNT40 = 2
  65.             Windows2000 = 3
  66.         End Enum
  67. #End Region
  68.  
  69. #Region " Public methods "
  70.         ''' <summary>
  71.         '''
  72.         ''' </summary>
  73.         ''' <param name="UserName"></param>
  74.         ''' <param name="Domain"></param>
  75.         ''' <param name="Password"></param>
  76.         ''' <returns></returns>
  77.         ''' <remarks></remarks>
  78.         <SecurityPermission(SecurityAction.Demand, ControlPrincipal:=True, UnmanagedCode:=True)> _
  79.         Private Shared Function GetWindowsIdentity(ByVal UserName As String, ByVal Domain As String, ByVal Password As String) As WindowsIdentity
  80.             Dim SecurityToken As Integer
  81.             Dim Success As Boolean
  82.             Success = LogonUser(UserName, Domain, Password, Logon.NetworkCleartext, Provider.Windows2000, SecurityToken)
  83.             If Not Success Then
  84.                 Throw New Exception("Logon Failed. Error: " & GetLastError())
  85.             End If
  86.             GetWindowsIdentity = New WindowsIdentity(New IntPtr(SecurityToken))
  87.         End Function
  88.  
  89.         ''' <summary>
  90.         '''
  91.         ''' </summary>
  92.         ''' <param name="Impersonate"></param>
  93.         ''' <remarks></remarks>
  94.         Public Shared Sub ImpersonateAdministrator(ByVal Impersonate As Boolean)
  95.             Dim newidentity As WindowsIdentity
  96.             Dim _Settings As New Impersination
  97.             If Impersonate = True Then
  98.                 newidentity = GetWindowsIdentity(_Settings.Username, _Settings.Domain, _Settings.Password)
  99.                 NewContext = newidentity.Impersonate
  100.             Else
  101.                 NewContext.Undo()
  102.             End If
  103.         End Sub
  104. #End Region
  105.  
  106.     End Class
  107. End Namespace

386 Rating: 1.3/5 (3 votes cast)