Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if app is run by sudo/admin on Linux #1244

Closed
maroallegro opened this issue Jan 29, 2018 · 1 comment
Closed

Check if app is run by sudo/admin on Linux #1244

maroallegro opened this issue Jan 29, 2018 · 1 comment

Comments

@maroallegro
Copy link

maroallegro commented Jan 29, 2018

I am trying to check if .NetCore code is running as sudo/admin on Linux. It works on Windows but throws exception on Linux.

How do I check if app is run by admin/sudo on Ubuntu Linux by using .NET Core2.0 build-in class?

Here is the code I have tried (however i thought that it will not manage admin rights on Linux):

using System;
using System.Security.Principal;

namespace smallTestsCore
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Program.IsAdministrator);
            Console.ReadLine();
        }
        public static bool IsAdministrator =>
            new WindowsPrincipal(WindowsIdentity.GetCurrent())
                .IsInRole(WindowsBuiltInRole.Administrator);
    }
}

The code works on Windows, but does not work on Linux:

Exception has occurred: CLR/System.PlatformNotSupportedException
An unhandled exception of type 'System.PlatformNotSupportedException' occurred in
System.Security.Principal.Windows.dll: 'Windows Principal functionality is not supported on this platform.'
  at System.Security.Principal.WindowsIdentity.GetCurrent()
   at adminTst.Program.get_IsAdministrator() in /home/user/adminTst/Program.cs:line 15
   at adminTst.Program.Main(String[] args) in /home/user/adminTst/Program.cs:line 11
@Petermarcu
Copy link
Member

Issue moved to dotnet/corefx #27304 via ZenHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants