Start application as Administrator.

Hello,

Is it possible to run a program as a different user, like Administrator.

For example: I define the Administrators password in the program, the program starts, and then runs
with Administrator priveleges.

I know about the option 'Run as', but i want the program to start at startup, run as an Administrator and do his work.

Thanks in advance.



Answer this question

Start application as Administrator.

  • Sudipto

    Hi,

    Yes, this is working fine, now i can continue working on my program.

    Do you have any idea for 'hiding' the Administrator password in the code

    Edit:
    I also use a SQL Database for my settings, maybe i should store the Administrator password in the database.


    Many thanks,


  • kunalb

    Hi,

    Probably I have messed up the code trying to present it in more readable form. Here it is again without _

    Imports System.Security.Principal

    Imports System.Runtime.InteropServices

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim token As IntPtr

    If Not AdvApi32.LogonUser("testUser", "testMachine", "testPassword", AdvApi32.LogonType.LOGON32_LOGON_INTERACTIVE, AdvApi32.LogonProvider.LOGON32_PROVIDER_DEFAULT, token) Then

    MessageBox.Show("Failed to login as user test")

    Exit Sub

    End If

    WindowsIdentity.Impersonate(token)

    MessageBox.Show(WindowsIdentity.GetCurrent().Name)

    'your code here

    End Sub

    End Class

    Public Class AdvApi32

    Public Enum LogonType

    LOGON32_LOGON_INTERACTIVE = 2

    LOGON32_LOGON_NETWORK = 3

    LOGON32_LOGON_BATCH = 4

    LOGON32_LOGON_SERVICE = 5

    LOGON32_LOGON_UNLOCK = 7

    LOGON32_LOGON_NETWORK_CLEARTEXT = 8

    LOGON32_LOGON_NEW_CREDENTIALS = 9

    End Enum

    Public Enum LogonProvider

    LOGON32_PROVIDER_DEFAULT = 0

    LOGON32_PROVIDER_WINNT35 = 1

    LOGON32_PROVIDER_WINNT40 = 2

    LOGON32_PROVIDER_WINNT50 = 3

    End Enum

    Declare Ansi Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" (ByVal pszUsername As String, ByVal pszDomain As String, ByVal pszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Boolean

    End Class



  • _Quimbly_

    Hi,

    Before I start I have to warn you that if you store the password as cleartext in your code you make it available to anyone with ildasm utility and your binary.

    Now here is the code:

    Imports System.Security.Principal

    Imports System.Runtime.InteropServices

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) _

    Handles Button1.Click

    Dim token As IntPtr

    If Not AdvApi32.LogonUser( _

    "testUser", "testMachine", "testPassword", _

    AdvApi32.LogonType.LOGON32_LOGON_INTERACTIVE, _

    AdvApi32.LogonProvider.LOGON32_PROVIDER_DEFAULT, _

    token) Then

    MessageBox.Show("Failed to login as user test")

    Exit Sub

    End If

    WindowsIdentity.Impersonate(token)

    MessageBox.Show(WindowsIdentity.GetCurrent().Name)

    'your code here

    End Sub

    End Class

    Public Class AdvApi32

    Public Enum LogonType

    LOGON32_LOGON_INTERACTIVE = 2

    LOGON32_LOGON_NETWORK = 3

    LOGON32_LOGON_BATCH = 4

    LOGON32_LOGON_SERVICE = 5

    LOGON32_LOGON_UNLOCK = 7

    LOGON32_LOGON_NETWORK_CLEARTEXT = 8

    LOGON32_LOGON_NEW_CREDENTIALS = 9

    End Enum

    Public Enum LogonProvider

    LOGON32_PROVIDER_DEFAULT = 0

    LOGON32_PROVIDER_WINNT35 = 1

    LOGON32_PROVIDER_WINNT40 = 2

    LOGON32_PROVIDER_WINNT50 = 3

    End Enum

    Declare Ansi Function LogonUser Lib "advapi32.dll" _

    Alias "LogonUserA" (ByVal pszUsername As String, _

    ByVal pszDomain As String, _

    ByVal pszPassword As String, _

    ByVal dwLogonType As Integer, _

    ByVal dwLogonProvider As Integer, _

    ByRef phToken As IntPtr) As Boolean

    End Class



  • BugEyeMonster

    Thanks for your help, but i get errors with the 'ByVal', 'Handles' and the 'Alias' functions.

            Alias "LogonUserA" (ByVal pszUsername As String, _

                                ByVal pszDomain As String, _

                                ByVal pszPassword As String, _

                                ByVal dwLogonType As Integer, _

                                ByVal dwLogonProvider As Integer, _

                                ByRef phToken As IntPtr) As Boolean

    This is also going wrong, by extending the line on the next line, if i put everything on one line, without the _ sign, it works, but still Alias, ByVal and Handles give an Syntax error.

    Do i need to add a reference maybe

    I used VS 2003 and 2005.

    Thanks in advance.

     

    --- Edit ---

    The ByVal and Handles error are fixed by removing the _ sign for the line break.
    Alias still gives a syntax error.

    And the:

    If Not AdvApi32.LogonUser("testUser", "testMachine", "testPassword", AdvApi32.LogonType.LOGON32_LOGON_INTERACTIVE, AdvApi32.LogonProvider.LOGON32_PROVIDER_DEFAULT, token) Then

    line gives the following error after compiling, "PInvoke restriction: cannot return variants."


  • Start application as Administrator.