-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSO.NetAPI32.pas
More file actions
80 lines (64 loc) · 1.57 KB
/
SSO.NetAPI32.pas
File metadata and controls
80 lines (64 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
unit SSO.NetAPI32;
interface
uses
SSO.Interfaces;
type
TSSONetAPI32Query = class(TInterfacedObject, ISSOQuery)
private
FLastError: string;
public
function TryGetUsername(var Username: string; var Domain: string): Boolean;
function LastError: string;
end;
implementation
uses
WinAPI.Windows,
System.SysUtils;
function NetApiBufferFree(Buffer: pointer): DWORD; stdcall; external 'netapi32.dll' name 'NetApiBufferFree';
function NetWkstaUserGetInfo(ServerName: PWideChar; Level: DWORD; var Buffer: Pointer): Longint; stdcall; external 'netapi32.dll' name 'NetWkstaUserGetInfo';
type
TUserInfo = record
UserName: PWideChar;
DomainName: PWideChar;
OtherDomainNames: PWideChar;
ServerName: PWideChar;
end;
PUserInfo = ^TUserInfo;
function TryGetUPNViaNetApi(var UPN: string): Boolean;
var
Userinfo: PUserInfo;
begin
Result := False;
if NetWkstaUserGetInfo(nil, 1, Pointer(Userinfo)) = 0 then
begin
try
UPN := WideCharToString(Userinfo^.UserName);
Result := True;
finally
NetApiBufferFree(Userinfo);
end;
end;
end;
function TSSONetAPI32Query.LastError: string;
begin
Result := FLastError;
end;
function TSSONetAPI32Query.TryGetUsername(var Username, Domain: string): Boolean;
var
UPN: string;
begin
Result := False;
if TryGetUPNViaNetApi(UPN) then
begin
Result := TUPN.TryToSplit(UPN, Username, Domain);
if not Result then
begin
FLastError := 'UPN format not recognized: ' + UPN;
end;
end
else
begin
FLastError := 'Call to NetWkstaUserGetInfo failed';
end;
end;
end.