源码工作室

目标:通俗的语言说出通俗的技术
随笔 - 99, 文章 - 0, 评论 - 1183, 阅读 - 79万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

C# 如何检查当前用户在Windows系统下的权限

Posted on   源码工作室  阅读(2436)  评论(1编辑  收藏  举报
System.Security.Principal.WindowsIdentity wid = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal printcipal = new System.Security.Principal.WindowsPrincipal(wid);
bool isAdmin=(printcipal .IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator));

如果 isAdmin 为true,则是在管理员权限下。

 

namespace System.Security.Principal
{
    // Summary:
    //     Specifies common roles to be used with System.Security.Principal.WindowsPrincipal.IsInRole(System.String).
    [Serializable]
    [ComVisible(true)]
    public enum WindowsBuiltInRole
    {
        // Summary:
        //     Administrators have complete and unrestricted access to the computer or domain.
        Administrator = 544,
        //
        // Summary:
        //     Users are prevented from making accidental or intentional system-wide changes.
        //     Thus, users can run certified applications, but not most legacy applications.
        User = 545,
        //
        // Summary:
        //     Guests are more restricted than users.
        Guest = 546,
        //
        // Summary:
        //     Power users possess most administrative permissions with some restrictions.
        //     Thus, power users can run legacy applications, in addition to certified applications.
        PowerUser = 547,
        //
        // Summary:
        //     Account operators manage the user accounts on a computer or domain.
        AccountOperator = 548,
        //
        // Summary:
        //     System operators manage a particular computer.
        SystemOperator = 549,
        //
        // Summary:
        //     Print operators can take control of a printer.
        PrintOperator = 550,
        //
        // Summary:
        //     Backup operators can override security restrictions for the sole purpose
        //     of backing up or restoring files.
        BackupOperator = 551,
        //
        // Summary:
        //     Replicators support file replication in a domain.
        Replicator = 552,
    }
}

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示