How can I get the local group name for guests/administrators?

The names of groups are localised depending on system language.
For 'well known' groups like 'Administrators' and 'Guests' you should retrieve based on the SID. The SID for Guests is:
S-1-5-32-546
There is a list of well known SIDs here:
http://support.microsoft.com/kb/243330


Looking up the account by SID is the best way to go. It's a bit contrived, but the way it works is this:
The Administrator account's SID always starts with S-1-5-21 and ends with -500. Everything else in-between is random (the domain's SID).

The Guest account's SID always starts with S-1-5-21 and ends with -501.


System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
var sid = currentUser.User.ToString().ToUpper();
 if (sid.StartsWith("S-1-5-21") && sid.EndsWith("-501"))
 {
   return "Guest";

 }


WindowsPrincipal wp = 
                new WindowsPrincipal(currentUser);
 
            Console.WriteLine(wp.IsInRole(
                WindowsBuiltInRole.Guest));
 
            Console.WriteLine(wp.IsInRole(
                WindowsBuiltInRole.Administrator));
 
            Console.WriteLine(wp.IsInRole(
                WindowsBuiltInRole.PowerUser));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值