![](/img/trans.png)
[英]How to get the current logged on user, including domain in Delphi 2009?
[英]How to get the user id and groups of the currently logged-in user in a client application of RAD Server in Delphi?
我需要在 RAD 服務器的客戶端應用程序中知道當前登錄用戶的用戶 ID 和組,以執行一些進一步的邏輯。 但我在文檔中找不到任何相關內容。 任何人都可以分享任何想法嗎? 謝謝。
獲取用戶用戶的用戶名:
function GetUserNameStringEx(UsernameFormat: EXTENDED_NAME_FORMAT): string;
var
szBuffer: UnicodeString;
nSize: Cardinal;
const
UNLEN = 256; //characters
begin
(*
Get the user and computer name
NameFullyQualifiedDN 'CN=Ian Boyd,OU=Contoso Users,DC=contoso,DC=com'
NameSamCompatible 'CONTOSO\ian' <--the only one supported if the user account is not in a domain
NameDisplay 'Ian'
NameUniqueId '{e756aad8-2630-4661-a362-abe75b96146a}'
NameCanonical 'contoso.com/Contoso Users/Ian Boyd'
NameUserPrincipal 'ian@Contoso.com'
NameCanonicalEx 'contoso.com/Contoso Users'#$A'Ian Boyd'
NameServicePrincipal ''
NameDnsDomain 'CONTOSO.COM\ian'
The value *cannot* be NameUnknown
*)
Result := '';
SetLength(szBuffer, 4096); //UNLEN+1);
nSize := Length(szBuffer);
if not GetUserNameEx(UsernameFormat, PWideChar(@szBuffer[1]), {var}nSize) then
Exit;
SetLength(szBuffer, nSize);
Result := szBuffer;
end;
獲取當前用戶的安全標識符 (SID)
function GetCurrentUserSID: string;
type
PTOKEN_USER = ^TOKEN_USER;
_TOKEN_USER = record
User: TSidAndAttributes;
end;
TOKEN_USER = _TOKEN_USER;
var
hToken: THandle;
cbBuf: Cardinal;
ptiUser: PTOKEN_USER;
bSuccess: Boolean;
begin
Result := '';
// Get the calling thread's access token.
if not OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, True, hToken) then
begin
if (GetLastError <> ERROR_NO_TOKEN) then
Exit;
// Retry against process token if no thread token exists.
if not OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken) then
Exit;
end;
try
// Obtain the size of the user information in the token.
bSuccess := GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf);
ptiUser := nil;
try
while (not bSuccess) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) do
begin
ReallocMem(ptiUser, cbBuf);
bSuccess := GetTokenInformation(hToken, TokenUser, ptiUser, cbBuf, cbBuf);
end;
Result := SidToString(ptiUser.User.Sid);
finally
FreeMem(ptiUser);
end;
finally
CloseHandle(hToken);
end;
end;
獲取組
(但請注意,有些組只是標簽,有些是允許,有些是拒絕)
var
hAccessToken: THandle;
ptgGroups: PTokenGroups;
dwInfoBufferSize: DWORD;
x: Integer;
bSuccess: BOOL;
begin
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 not bSuccess then
Exit;
GetMem(ptgGroups, 1024);
bSuccess := GetTokenInformation(hAccessToken, TokenGroups, ptgGroups, 1024, dwInfoBufferSize);
CloseHandle(hAccessToken);
if not bSuccess then
Exit;
{$R-}
for x := 0 to ptgGroups^.GroupCount - 1 do
begin
Here you can do something with:
ptgGroups^.Groups[x]
end;
FreeMem(ptgGroups);
end;
從 RAD 服務器獲取有關用戶/組的信息的一種方法是使用它的管理 API,例如RetrieveUser 。 這很多,但是閱讀內部 API 邏輯將對您來說還有很長的路要走。 祝你好運。
注意:使用 TEMSClientAPI、TBackendUsers 或 TBackendAuth 組件從您的 RAD 服務器數據庫中注冊、登錄、檢索、更新或刪除 RAD 服務器用戶信息
BackendAuth 有一個名為 UserDetails 的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.