How to determine if a user can run admin?

When a user must enter his license key, we want to put it in HKLM, if we can, and in HKCU, if we can not. If it is located in HKLM, then all users on the computer have a license, each of which does not have to enter it.

We are AddOn for Office, so we work with Office rights. As a rule, these are not administrator rights (unless they have disabled the UAC). Thus, WindowsPrincipal.IsInRole (Administrator) will return false, regardless of what the user can do.

If the user has local administrator rights, we want to run the applet that has runas = admin, and then he can install it in HKLM. However, if they do not have local administrator rights, we put them in HKCU.

So ... How to determine if a user can runas = admin? We are on .net 3.5.

thanks - dave

+3
source share
1 answer

The process that I usually use in some client programs that we write looks like this:

  • Attempted to run an elevated process to install registry keys.
  • Wait for the process to complete or be excluded.
  • Confirm registry keys were set by reading the expected keys (non-admin can do this)
  • If the keys were not installed, perform the backup method (for example, write to HKCU)

, (VB.Net). , , . .

Private Function RunElevated(commandLine As String, Optional ByVal timeout As Integer = 0) As Boolean
    Dim startInfo As New ProcessStartInfo
    startInfo.UseShellExecute = True
    startInfo.WorkingDirectory = Environment.CurrentDirectory
    Dim uri As New Uri(Assembly.GetEntryAssembly.GetName.CodeBase)
    startInfo.FileName = uri.LocalPath
    startInfo.Verb = "runas"
    startInfo.Arguments = commandLine

    Dim success As Boolean
    Try
        Dim p As Process = Process.Start(startInfo)
        ' wait thirty seconds for completion
        If timeout > 0 Then
            If Not p.WaitForExit(30000) Then
                ' did not complete in thirty seconds, so kill
                p.Kill()
                success = False
            Else
                success = True
            End If
        Else
            p.WaitForExit()
            success = True
        End If
    Catch ex As Win32Exception
        success = False
    Catch ex As Exception
        MsgBox("Error occurred while trying to start application as administrator: " & ex.Message)
        success = False
    End Try
    Return success
End Function

, 30 . , , .

, , :

Public Function IsAdmin() As Boolean
    Dim id As WindowsIdentity = WindowsIdentity.GetCurrent
    Dim p As New WindowsPrincipal(id)
    Return p.IsInRole(WindowsBuiltInRole.Administrator)
End Function

, , . , , , . RunElevated , , . :

Public Function UpdateSettings(...) As Boolean
    Dim success As Boolean
    Try
        If Not IsAdmin() Then
            ' try to create the registry keys as administrator
            success = RunElevated(Command() & " /admin", 30000)
        Else
            ' if we're already admin, then just update directly
            success = UpdateSettingsAdmin(...)
        End If
        success = success And ValidateUpdateSettings(...)
    Catch ex As Exception
        success = False
    End Try
    Return success
End Function
+2

All Articles