Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to create Folder when you do not have rights but have admin userName and pwd

How to create Folder when you do not have rights but have admin userName and pwd

Scheduled Pinned Locked Moved Visual Basic
comsecuritytutorialworkspace
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rizwan Bashir
    wrote on last edited by
    #1

    Hello There My application is running in an environment where security policy does not allow the application to create folder on d drive. but my Application does have the Administrator UserName and Password. I want to create folder using Admin Credentials or you can say my application should run with Admin Rights no matter what ever the rights are for the logged on user. Any helpful code or any Arcticle link would be highly appreciated. Regards

    Rizwan Bashir ALM Soft[^]

    S 1 Reply Last reply
    0
    • R Rizwan Bashir

      Hello There My application is running in an environment where security policy does not allow the application to create folder on d drive. but my Application does have the Administrator UserName and Password. I want to create folder using Admin Credentials or you can say my application should run with Admin Rights no matter what ever the rights are for the logged on user. Any helpful code or any Arcticle link would be highly appreciated. Regards

      Rizwan Bashir ALM Soft[^]

      S Offline
      S Offline
      SHatchard
      wrote on last edited by
      #2

      Found this code, its not tested but gives you a pointer in imperonating another user.

      Imports System
      Imports System.Runtime.InteropServices
      Imports System.Security.Principal
      Imports System.Security.Permissions

      Public Class Impersonation

      _
      Public Shared Function LogonUser(lpszUsername As String, lpszDomain As
      String, lpszPassword As String, _
      dwLogonType As Integer, dwLogonProvider As Integer, ByRef
      phToken As Integer) As Boolean
      End Function

      _
      Public Shared Function GetLastError() As Integer
      End Function

      Public Shared Sub Main(args() As String)

      'The Windows NT user token.
      Dim token1 As Integer

      'Get the user token for the specified user, machine, and password
      using the unmanaged LogonUser method.

      'The parameters for LogonUser are the user name, computer name,
      password,
      'Logon type (LOGON32_LOGON_NETWORK_CLEARTEXT), Logon provider
      (LOGON32_PROVIDER_DEFAULT),
      'and user token.
      Dim loggedOn As Boolean = LogonUser("bob", "AARDVARK", "coffee", 3, 0,
      token1)
      Console.WriteLine("LogonUser called")

      'Call GetLastError to try to determine why logon failed if it did not
      succeed.
      Dim ret As Integer = GetLastError()

      Console.WriteLine("LogonUser Success? " + loggedOn)
      Console.WriteLine("NT Token Value: " + token1)
      If ret <> 0 Then
      Console.WriteLine("Error code (126 == ""Specified module could not
      be found""): " + ret)
      End If

      'Starting impersonation here:
      Console.WriteLine("Before impersonation:")
      Dim mWI1 As WindowsIdentity = WindowsIdentity.GetCurrent()
      Console.WriteLine(mWI1.Name)
      Console.WriteLine(mWI1.Token)

      Dim token2 As IntPtr = new IntPtr(token1)

      Console.WriteLine("New identity created:")
      Dim mWI2 As WindowsIdentity = new WindowsIdentity(token2)
      Console.WriteLine(mWI2.Name)
      Console.WriteLine(mWI2.Token)

      'Impersonate the user.
      Dim mWIC As WindowsImpersonationContext = mWI2.Impersonate()

      Console.WriteLine("After impersonation:")
      Dim mWI3 As WindowsIdentity = WindowsIdentity.GetCurrent()
      Console.WriteLine(mWI3.Name)
      Console.WriteLine(mWI3.Token)

      'Revert to previous identity.
      mWIC.Undo()

      Console.WriteLine("After impersonation is reverted:")
      Dim mWI4 As WindowsIdentity = WindowsIdentity.GetCurrent()
      Console.WriteLine(mWI4.Name)
      Console.WriteLine(mWI4.Token)
      End Sub
      End Class

      R 1 Reply Last reply
      0
      • S SHatchard

        Found this code, its not tested but gives you a pointer in imperonating another user.

        Imports System
        Imports System.Runtime.InteropServices
        Imports System.Security.Principal
        Imports System.Security.Permissions

        Public Class Impersonation

        _
        Public Shared Function LogonUser(lpszUsername As String, lpszDomain As
        String, lpszPassword As String, _
        dwLogonType As Integer, dwLogonProvider As Integer, ByRef
        phToken As Integer) As Boolean
        End Function

        _
        Public Shared Function GetLastError() As Integer
        End Function

        Public Shared Sub Main(args() As String)

        'The Windows NT user token.
        Dim token1 As Integer

        'Get the user token for the specified user, machine, and password
        using the unmanaged LogonUser method.

        'The parameters for LogonUser are the user name, computer name,
        password,
        'Logon type (LOGON32_LOGON_NETWORK_CLEARTEXT), Logon provider
        (LOGON32_PROVIDER_DEFAULT),
        'and user token.
        Dim loggedOn As Boolean = LogonUser("bob", "AARDVARK", "coffee", 3, 0,
        token1)
        Console.WriteLine("LogonUser called")

        'Call GetLastError to try to determine why logon failed if it did not
        succeed.
        Dim ret As Integer = GetLastError()

        Console.WriteLine("LogonUser Success? " + loggedOn)
        Console.WriteLine("NT Token Value: " + token1)
        If ret <> 0 Then
        Console.WriteLine("Error code (126 == ""Specified module could not
        be found""): " + ret)
        End If

        'Starting impersonation here:
        Console.WriteLine("Before impersonation:")
        Dim mWI1 As WindowsIdentity = WindowsIdentity.GetCurrent()
        Console.WriteLine(mWI1.Name)
        Console.WriteLine(mWI1.Token)

        Dim token2 As IntPtr = new IntPtr(token1)

        Console.WriteLine("New identity created:")
        Dim mWI2 As WindowsIdentity = new WindowsIdentity(token2)
        Console.WriteLine(mWI2.Name)
        Console.WriteLine(mWI2.Token)

        'Impersonate the user.
        Dim mWIC As WindowsImpersonationContext = mWI2.Impersonate()

        Console.WriteLine("After impersonation:")
        Dim mWI3 As WindowsIdentity = WindowsIdentity.GetCurrent()
        Console.WriteLine(mWI3.Name)
        Console.WriteLine(mWI3.Token)

        'Revert to previous identity.
        mWIC.Undo()

        Console.WriteLine("After impersonation is reverted:")
        Dim mWI4 As WindowsIdentity = WindowsIdentity.GetCurrent()
        Console.WriteLine(mWI4.Name)
        Console.WriteLine(mWI4.Token)
        End Sub
        End Class

        R Offline
        R Offline
        Rizwan Bashir
        wrote on last edited by
        #3

        Thanks will let you know the output as soon as I shall test this. Regards

        Rizwan Bashir ALM Soft[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups