windows活动目录和域相关问题,急

zzn007 2009-03-02 11:19:34
现在有一验证域用户的需求。环境是这样的:服务器是多个域,他们组成域树,如根域是anli.com,其子域是sh.anli.com.
目前,每个客户端都登陆到相应的域中。现在的需求是:通过客户域验证,并需要获取指定域中的所有组织单元和用户的详细信息!
这几天我再网上搜索了一些资料,仅仅搞懂了一小部分。不解的地方,需要高手解决:
1,客户端,我要获取当前登陆的用户名称和域(注意是域的全程)。我可以或用户和域名,但是域名是简称,如域sh.anli.com,我只能获取到域名是sh,如何获取sh.anli.com?

2,我使用ADsOpenObject方法,遍历指定域下的所有组织单元和用户,但是通过接口,我只能获取用户和组织单元的名称,其他属性我怎么获取?
如账户名是ADsObj.Get('sAMAccountName'),属性“sAMAccountName”是账户,别的属性是什么?这些属性从哪里得到的?
还有组织单元,仅仅有Name属性,别的属性我也不知道,怎么才能知道这些属性?

以上都是我从例子中获取的,不知道属性他们是怎么知道的?我尝试的时候,将ADsObj强制转换为IADSUser,然后使用IADSUser下面的属性,有的会报异常信息,请问怎么知道这个属性没有数值???
...全文
114 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzn007 2009-03-13
  • 打赏
  • 举报
回复
以上方法无法获取登录的当前的域名称,还有那些答案吗?
现在客户端可以登录根域也可以登录子域,还可以登录本机,请问如何获取登录的当前的域的全面?我只能获取短名称,无法获取DNS域名
金卯刀 2009-03-13
  • 打赏
  • 举报
回复
關于活動目錄的問題,參考這個
http://www.agnisoft.com/white_papers/active_directory.asp
金卯刀 2009-03-13
  • 打赏
  • 举报
回复
關于問題1,參考如下代碼:

//**********************************************************************
//
// FUNCTION: GetCurrentUserAndDomain - This function looks up
// the user name and domain name for the user account
// associated with the calling thread.
//
// PARAMETERS: szUser - a buffer that receives the user name
// pcchUser - the size, in characters, of szUser
// szDomain - a buffer that receives the domain name
// pcchDomain - the size, in characters, of szDomain
//
// RETURN VALUE: TRUE if the function succeeds. Otherwise, FALSE and
// GetLastError() will return the failure reason.
//
// If either of the supplied buffers are too small,
// GetLastError() will return ERROR_INSUFFICIENT_BUFFER
// and pcchUser and pcchDomain will be adjusted to
// reflect the required buffer sizes.
//
//**********************************************************************

function GetCurrentUserAndDomain(szUser : PChar; pcchUser : DWORD;
szDomain : PChar; pcchDomain: DWORD) : boolean;

var
fSuccess : boolean;
hToken : THandle;
ptiUser : PSIDAndAttributes;
cbti : DWORD;
snu : SID_NAME_USE;

begin
ptiUser := nil;
Result := false;

try
// 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;

// Obtain the size of the user information in the token.
if (GetTokenInformation(hToken, TokenUser, nil, 0, cbti)) then

// Call should have failed due to zero-length buffer.
Exit

else

// Call should have failed due to zero-length buffer.
if (GetLastError() <> ERROR_INSUFFICIENT_BUFFER) then
Exit;


// Allocate buffer for user information in the token.
ptiUser := HeapAlloc(GetProcessHeap(), 0, cbti);
if (ptiUser= nil) then
Exit;

// Retrieve the user information from the token.
if ( not GetTokenInformation(hToken, TokenUser, ptiUser, cbti, cbti)) then
Exit;

// Retrieve user name and domain name based on user's SID.
if ( not LookupAccountSid(nil, ptiUser.Sid, szUser, pcchUser,
szDomain, pcchDomain, snu)) then
Exit;

fSuccess := TRUE;

finally

// Free resources.
if (hToken > 0) then
CloseHandle(hToken);

if (ptiUser <> nil) then
HeapFree(GetProcessHeap(), 0, ptiUser);
end;

Result := fSuccess;
end;
hanks_gao 2009-03-03
  • 打赏
  • 举报
回复
當前登錄的域,可以參考下列代碼:

function getComputerName():String;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
addr: dword; strIP:String ;
begin
strIP := '127.0.0.1';
WSAStartup(2, WSAData);

try
addr := inet_addr(PChar(strIP));
HostEnt:= GetHostByAddr(@addr, Length(strIP), PF_INET);
result:=HostEnt.h_name;
except
result := '';
end;

WSACleanup;
end;
hanks_gao 2009-03-03
  • 打赏
  • 举报
回复
當前登陸的用戶名:

function NowWinUserName: string;
var
FStr: PChar;
FSize: Cardinal;
begin
FSize := 255;
GetMem(FStr, FSize);
GetUserName(FStr, FSize);
Result := FStr;
FreeMem(FStr);
end;
zzn007 2009-03-03
  • 打赏
  • 举报
回复
还没人能解决吗?
zzn007 2009-03-02
  • 打赏
  • 举报
回复
看到一个API的方法,但是VB和VC的,我试过n回总报异常,那位大侠可以再delphi中使用通过。
API方法是GetComputerNameEx
lingyin55 2009-03-02
  • 打赏
  • 举报
回复
帮顶

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧