实现效果:
知识运用:
WindowsPrincipal类的IsInRole方法 //确定当前主体是否属于制定的Windows用户组
public virtual bool IsInRole (WindowsBuiltInRole role) //属性为枚举值之一
实现代码:
private void button1_Click(object sender, EventArgs e) { AppDomain domain = System.Threading.Thread.GetDomain(); //获取当前域 domain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);//将操作系统组映射到角色 //获取当前系统组 System.Security.Principal.WindowsPrincipal principal = (System.Security.Principal.WindowsPrincipal)System.Threading.Thread.CurrentPrincipal; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.User)) label1.Text += "当前用户为普通用户 "; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.PowerUser)) label1.Text += "当前用户为超级用户 "; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) label1.Text += "当前用户为系统管理员 "; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.SystemOperator)) label1.Text += "当前用户为系统操作员 "; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.BackupOperator)) label1.Text += "当前用户为备份操作员 "; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.PrintOperator)) label1.Text += "当前用户为打印操作员 "; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Replicator)) label1.Text += "当前用户为复制操作员 "; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.AccountOperator)) label1.Text += "当前用户为账户操作员 "; if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Guest)) label1.Text += "当前用户为来宾用户 "; }