0

I want to ask for administrator permissions at runtime in my application, but I couldn't find anything, despite the googling and searching. I could only find posts regarding rights at startup, I found one but, it was valid for .NET Framework, I can't really use dNF, I am using .NET Core 5.

Anyhow, how do I demand it? Maybe call some C functions from Win32 API? My application is Windows only, that's why I mentioned that. (yes, but I still can't port over from .net 5)

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • You can't. If you want to get the OS UAC prompt asking the user to use his/her admin mojo when running the program, only the OS can do that for you, and it is done before your code starts. When a program starts, it gets a token that identifies the user and his/her permissions and privileges. When UAC is enabled, an admin user has a non-admin token and an admin token. As a program starts it gets one of those. Once the program starts, the token cannot change. If you need to do this, start up another process with a different token. BTW, what is _"dNF"_? – Flydog57 Apr 23 '21 at 03:58
  • If you don't want the manifest flag, you can start a new process with `new ProcessStartInfo{ Verb = "runas", UseShellExecute = true, ... };` – Jeremy Lakeman Apr 23 '21 at 04:08
  • Oh, that's a bummer . Guess I'll have to start a new process in that case. Oh and, @Flydog57, `dNF` is short for `.NET Framework`. – ANF-Studios May 10 '21 at 01:00

1 Answers1

0

As far as my research (May 9, 2021) when, there is no possible way to extend the privileges of the current program. Also see @Flydog57's comment:

You can't. If you want to get the OS UAC prompt asking the user to use his/her admin mojo when running the program, only the OS can do that for you, and it is done before your code starts. When a program starts, it gets a token that identifies the user and his/her permissions and privileges. When UAC is enabled, an admin user has a non-admin token and an admin token. As a program starts it gets one of those. Once the program starts, the token cannot change. If you need to do this, start up another process with a different token.

The only logical solution is to start a new executable/process whole together with elevated privileges. With C#, you can use System.Diagnostics.Process.

However, this might change (I have no sources, but it's a possibility) in the future.