How to check if a given user is a member of the built-in Administrators group?

后端 未结 4 1706
刺人心
刺人心 2021-01-15 03:28

I need to check programmatically (in .NET) whether a given user (domain account) is a member of the built-in Administrators group on a current computer (the one where the ap

4条回答
  •  不要未来只要你来
    2021-01-15 03:51

    If you are talking about the currently running user then

    using System.Security.Principal;
    
    WindowsIdentity identity = WindowsIdentity.GetCurrent();
    WindowsPrincipal wp = new WindowsPrincipal(identity);
    
    if (wp.IsInRole("BUILTIN\Administrators"))
       // Is Administrator
    else
       // Is Not
    

    If not then I expect its possible to set identity to a particular user but not looked into how.

提交回复
热议问题