4

現在の Windows ユーザーがローカル管理者であるか、UAC を使用してそのグループ メンバーシップを「取得」できるかを調べようとしています。

私がこれまでに思いついたことは、次のようになります。

var adminIdentifier = new SecurityIdentifier("S-1-5-32-544");
var current = WindowsIdentity.GetCurrent();
bool isAdmin = current.Groups.Contains(adminIdentifier);
bool canBeAdmin = isAdmin;

if (!isAdmin)
{
    var adminGroupName = adminIdentifier.Translate(typeof(NTAccount)).Value;
    adminGroupName = adminGroupName.Substring(adminGroupName.LastIndexOf('\\'));
    string path = "WinNT://./" + adminGroupName + ",group";

    using (DirectoryEntry groupEntry = new DirectoryEntry(path))
    {
      foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
      {
        using (DirectoryEntry memberEntry = new DirectoryEntry(member))
        {
              object obVal = memberEntry.Properties["objectSid"].Value;
              SecurityIdentifier sid = null;
              if (null != obVal)
              {
                 sid = new SecurityIdentifier((Byte[])obVal,0);
              }

              canBeAdmin = Equals(current.User, sid);
              if (canBeAdmin)
                break;
        }
     }
   }
 }
 Console.WriteLine(canBeAdmin +" "+isAdmin);

このソリューションの計算には数ミリ秒かかります。以前に試した System.DirectoryServices.AccountManagement ベースのアプローチよりもはるかに高速です。

ただし、最後に気になることが 1 つあります。管理者グループの SecurityIdentifier を名前に変換する必要があります。SID を使用して DirectoryEntry を直接取得する方法が必要です。Googleによると、これはうまくいくはずです:

string path = "LDAP://<SID=" + adminIdentifier.ToString() + ">";

ただし、これは機能しないようです。構文がどのように見えるべきか考えていますか?

4

3 に答える 3

2

WindowsPrincipal.IsInRoleを試しましたか?

WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
wp.IsInRole(new SecurityIdentifier("S-1-5-32-544"));
wp.IsInRole(WindowsBuiltInRole.Administrator);
于 2009-04-05T12:41:16.407 に答える
1

現在のユーザーに対応するUserPrincipalオブジェクトでGetAuthorizationGroupsを使用してみましたか?GetAuthorizationGroupsにマシンローカルグループが含まれていない場合は、ユーザーが直接ローカル管理者グループに属しているかどうか、またはローカル管理者グループにユーザーが属している許可グループが含まれているかどうかを確認する必要があります。私はマシンコンテキストでこれを試したことがないので、ドメイン/グローバル/ユニバーサルグループを使用してローカルグループメンバーシップを計算しない場合、後者の一致についてドメインコンテキストも検索する必要があるかどうかはわかりませんマシンコンテキスト。

于 2009-04-04T12:14:37.790 に答える
0

私が誤解しない限り、これはあなたが望むものでなければなりません:

var groupName = adminIdentifier.Translate(typeof(NTAccount)).Value;
于 2009-04-05T12:19:29.657 に答える