【发布时间】: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 的组管理员中代表管理员启动,则所有请求都是相同的。
所以对我来说是必要的:
- 要了解,启动程序的用户属于 经理(本地或域)正常工作
- 代表系统记帐记录“管理员”(可以并重命名)启动,而不是创建具有管理员权限的新用户
[更新]
再一次,以另一种方式。我们承认,系统中有一些账户: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