检测管理员帐户

时间:2011-08-13 03:24:36

标签: delphi

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;

但它只将用户的附件定义为管理员组。如何定义,根据会计记录“管理员”的确切内容开始(考虑到记录名称可以更改的内容(帐户重命名,例如“管理员”)?

P.S。如果启动应用程序的用户是包含在组中的组管理员,则代表管理员启动的Windows UAC将是所有相同的请求。

所以我有必要:

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

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

    需要:

    Function GetCurrentUserName:string;
    begin
    ... detect current user name
    end;
    
    Function isCurrentUserisAdministratorPC:bool; 
    begin
    // ??? Result:=isUserPCAdmin(GetCurrentUserName);
    end;
    

    //使用

    User1启动程序:isCurrentUserisAdministratorPC返回False;

    User2启动程序:isCurrentUserisAdministratorPC返回False;

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

    将帐户管理员重命名为Test123。

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

2 个答案:

答案 0 :(得分:2)

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

您想知道用户是否拥有管理员privelages。我已经回答了here.

答案 1 :(得分:0)

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

  
    

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

  

如果包含Windows UAC,则管理员组的成员首先接收权限应在UAC的消息的新窗口中确认它(默认情况下包含在本地和组策略者Windows中)。个人计算机管理员的本地记录 - 不需要此类行为。

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

if IsUserAnAdmin then Showmessage('Admin') else Showmessage('Not Admin, or UAC enabled');
相关问题