【问题标题】:Detect Administrator account检测管理员帐户
【发布时间】:2011-08-13 03:24:36
【问题描述】:

DelphiXe,Win7x64

如何定义,用户启动程序代表系统管理员(域或本地)的系统记录启动它。我这样定义的权利:

Function IsUserAdmin:Bool;
Const 
  SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority =(Value: (0, 0, 0, 0, 0, 5));
  SECURITY_BUILTIN_DOMAIN_RID = $00000020;
  DOMAIN_ALIAS_RID_ADMINS = $00000220;
Var 
  hAccessToken: THandle;
  ptgGroups: PTokenGroups; 
  dwInfoBufferSize: DWORD; 
  psidAdministrators: PSID; 
  x: Integer;
  bSuccess: BOOL;
begin
  Result := False;
  bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken);
  if not bSuccess then 
  begin
    if GetLastError = ERROR_NO_TOKEN then 
      bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hAccessToken);
  end;

  if bSuccess then 
  begin 
    GetMem(ptgGroups, 1024);
    bSuccess := GetTokenInformation(hAccessToken, TokenGroups, ptgGroups, 
                                    1024, dwInfoBufferSize);
    CloseHandle(hAccessToken);
    if bSuccess then 
    begin
      AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
                               SECURITY_BUILTIN_DOMAIN_RID, 
                               DOMAIN_ALIAS_RID_ADMINS, 
                               0, 0, 0, 0, 0, 0, psidAdministrators);
      {$R-}
      for x := 0 to ptgGroups.GroupCount-1 do 
        if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then 
        begin 
          Result := True;
          Break;
        end;
      {$R+}
      FreeSid(psidAdministrators);
    end;
    FreeMem(ptgGroups);
  end;
end;

但它只定义了用户对管理员组的附件。如何定义,究竟是从会计记录“Administrator”下的什么开始(考虑到什么记录名称可以更改(帐户被重命名,例如“Admin”)?

附:这就是说,如果启动应用程序的用户在包含 Windows UAC 的组管理员中代表管理员启动,则所有请求都是相同的。

所以对我来说是必要的:

  1. 要了解,启动程序的用户属于 经理(本地或域)正常工作
  2. 代表系统记帐记录“管理员”(可以并重命名)启动,而不是创建具有管理员权限的新用户

[更新]

再一次,以另一种方式。我们承认,系统中有一些账户:Administrator(默认为管理员的系统账户)、User1(包含在“Administrators”组中,新创建的账户)、User2(包含在“Users”组中,新创建的帐户)。出于任何原因,系统帐户“管理员”在“管理员”中重命名(或任何其他名称)。有我的申请。它由不同的用户启动。对于我来说,启动我的应用程序的用户是系统管理员(Admin)。因为对于 Windows UAC,从 User1 和 Admin 启动的权限会有所不同 - 只有当应用程序启动 User1 时才会出现问题 UAC,如果是 Admin - 消息 UAC 将不会出现。这里有一个问题:如何定义,启动应用的用户=Admin(旧称Administrator),也就是说用户是系统的管理员?

需要:

Function GetCurrentUserName:string;
begin
... detect current user name
end;

Function isCurrentUserisAdministratorPC:bool; 
begin
// ??? Result:=isUserPCAdmin(GetCurrentUserName);
end;

// 使用

User1启动程序:isCurrentUserisAdministratorPC return False;

User2启动程序:isCurrentUserisAdministratorPC return False;

管理员启动程序:isCurrentUserisAdministratorPC 返回 TRUE; //!!!

将帐户 Admin 重命名为 Test123。

Test123 启动程序:isCurrentUserisAdministratorPC 返回 TRUE; //!!!

【问题讨论】:

  • 你想通过检测用户权限来完成什么?

标签: delphi


【解决方案1】:

该代码检查用户是否是Administrators 组的成员。某人可以是 Administrators 组的成员,但没有任何管理员权限。

您想知道用户是否真的拥有管理员权限。我已经回答了这个问题here.

【讨论】:

    【解决方案2】:

    找到了。通过内置记录中 level=1 的 NetUserEnum (http://msdn.microsoft.com/en-us/library/aa370652(VS.85).aspx),标志 66049(如果断开连接,则为 66051)将返回。

    伊恩·博伊德: 该代码检查用户是否是管理员组的成员。某人可以是管理员组的成员,但没有任何管理员权限。

    如果包含 Windows UAC,则管理员组成员首先应在来自 UAC 的消息的新兴窗口中确认它(默认情况下包含在本地和组政治家窗口中)。个人计算机管理员的本地记录 - 不需要此类操作。

    是的,它还检测到“IsUserAnAdmin”功能。

    if IsUserAnAdmin then Showmessage('Admin') else Showmessage('Not Admin, or UAC enabled');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      相关资源
      最近更新 更多