6

Is there a way to request elevated privileges from the os, for "just a part" of a c# program?

I'm writing a bunch of integrationtests using NUnit. One of the things I'm testing is if the application under test correctly connects to port 843. The test opens a listening socket at port 843, and then throws all sorts of responses to the application under test, and verifies if the application behaves correctly.

Opening a listening socket on port 843 requires admin privileges however.

I'd like to find the least intrusive way to be able to run this test. I could run the entire NUnit suite as root/admin, but that would make a lot of stuff run as root, that really doesn't need to be ran as root, which I'd like to prevent.

Lucas Meijer
  • 4,424
  • 6
  • 36
  • 53

4 Answers4

2

If required below code would help you to find out if the current logged in user is admin or not:

using System;
using System.Security.Principal; 

class Test
{
    public static void Main()
    {
        if (new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
        {
            Console.WriteLine("I am an admin.");
        }
    }
}
Brian Surowiec
  • 17,123
  • 8
  • 41
  • 64
Neo
  • 131
  • 8
0

Nope. Elevation is all or nothing. Typically if elevation is required, the app bootstraps itself into an elevated state.

Sky Sanders
  • 36,396
  • 8
  • 69
  • 90
  • 1
    That is incorrect. There is way using a [COM Elevation Moniker](https://learn.microsoft.com/en-us/windows/desktop/com/the-com-elevation-moniker). I am currently searching for an example in C#, but it looks like I'll have to try and implement it using the C++ examples... A different approach is is to use [Impersonation](https://stackoverflow.com/questions/3003417/how-do-i-use-impersonation-on-a-c-sharp-winforms-application-to-run-with-admin-p), but for that you need to login, prompting the user for a username and password which is not ideal. – Louis Somers Jan 29 '19 at 13:54
  • I found an example: [How to UAC elevate a COM component with .NET](https://stackoverflow.com/questions/127042/how-to-uac-elevate-a-com-component-with-net) – Louis Somers Jan 29 '19 at 14:03
0

Yes, you could take a look at the LogonUser function. Here's a sample.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I could be mistaken, but impersonation will not get past UAC. no? I know I have used it in the past on xp and nt but not v+ – Sky Sanders Feb 16 '10 at 09:16
  • I have been looking for this answer, I think it's possible to elevate only a single thread to perform some function, but I don't really know how – Chibueze Opata Jul 30 '12 at 17:11
0

A process has to be started with elevated privileges to have elevated rights. You cannot change your elevated status "in process".

A way to work around this is to do as Task Manager. If you run that "unelevated" and click on "Show processes for all users", it basically kills of the old task manager process and starts a new one with elevated privileges in order to do the job.

Arve
  • 7,284
  • 5
  • 37
  • 41