Solved

Windows NT Domains and Local System Account

Posted on 2003-11-23
2
586 Views
Last Modified: 2012-05-04
Greetings


Does anyone know how I can make a clear destinction between a user that logged into an NT domain and one that logs unto a local system account.

With my Microsoft client, I have the option to either log onto the domain or log in with a local system account.

I would like some code that will indicate whether I am on the domain or a local account.

Any pointers or samples will be greatly appreciated.

Thanks in advance.
 Engwi
0
Comment
Question by:Engwi
2 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
Comment Utility
Hi,

There should be an environment variable USERDOMAIN that shows the domain name (or local computer name) you are currently logged on. Get it like:

function GetEnvStr(EnvName: AnsiString): AnsiString;
var
  Buffer: Array[1..256] of Char;
begin
  EnvName:='%'+EnvName+'%'+#0;
  ExpandEnvironmentStrings(@EnvName[1],@Buffer,SizeOf(Buffer));
  Result:=StrPas(@Buffer);
  if (Result+#0=EnvName) then Result:='';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetEnvStr('USERDOMAIN'));
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
The best way would be by using GetTokenInformation to get the user token and then use LookupAccountSid to convert the token to a username/domain name pair. Something like:

var
  hAccessToken: THandle;
  UserToken: PSIDAndAttributes;
  InfoBufferSize: DWORD;
  dwInfoBufferSize: DWORD;
  vName, vDomain: PChar;
  cbName, cbDomain: DWORD;
  peUse: SID_NAME_USE;
begin
  if OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken) then begin
    UserToken := nil;
    GetTokenInformation(hAccessToken, TokenUser, UserToken, 0, InfoBufferSize);
    GetMem(UserToken, InfoBufferSize);
    if GetTokenInformation(hAccessToken, TokenUser, UserToken, InfoBufferSize, dwInfoBufferSize) then begin
      cbName := 0;
      cbDomain := 0;
      vName := nil;
      vDomain := nil;
      LookupAccountSid(nil, SID, vName, cbName, vDomain, cbDomain, peUse);
      LastError := GetLastError;
      if (LastError = ERROR_INSUFFICIENT_BUFFER) then begin
        GetMem(vName, cbName);
        GetMem(vDomain, cbDomain);
        if LookupAccountSid(nil, SID, vName, cbName, vDomain, cbDomain, peUse) then begin
        WriteLn(Log, 'User ', UserName, ' (', DisplayName, ') on domain ', Domain, '.');
      end;
    end;
  end;
end;

LookupAccountSid provides three types of information. The username and length of the username, the domain name and the length of the domain name and finally, the type of the account. Now, the domain name can be equal to the computer name, in which case it's a local account. Or it is the name of the domain.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now