Вопрос

I have a program that allows to login by using the windows login information, and I am trying to get the windows groups members when the user enter his password, I wrote a small function similar to my code :

procedure ShowADSPath(UserName, Password: widestring);
var Group :    IADs;
begin
  try
      OleCheck(ADsOpenObject('WinNT://Server/Group1',
       UserName,
       Password, ADS_SECURE_AUTHENTICATION, IADs, Group));
       if (Group <> nil) and (Group.Class_ = 'Group') then
         ShowMessage(Group.ADsPath);
       Group.release;  
       Group:= nil;
  except
     ShowMessage('NOT ACCESSDE');
  end;
end;

so when the entered username and password are right the program returns the path for the group when wrong 'NOT ACCESSED' appears.

the function works well if I enter the right username and password for the first time, or if I enter wrong username and password data it works fine too.

the problem is when I call the function second time it doesn't work as expected like:

when I run my program and first I enter wrong password and call my function 'NOT ACCESSED' will appear as expected, but if I recall the function even with the right password it returns 'NOT ACCESSED' too.

Also when I run my program and first I enter right password and call my function the groups path appears as expected, but if I recall the function with the wrong password it returns the path too.

it looks like my connection data became saved, and I need to free the memory but I don't know how.

any body can help?

Это было полезно?

Решение

Finally I could find a solution for my issue, which looks like a Microsoft API issue described in this article: http://support.microsoft.com/kb/218497

actually the API function ADsOpenObject is opening a connection with the server using the credentials you pass but it never close that connection, I tried to close it but it doesn't became close in the session, so I used another API to check the existence of the object first, check out this function it worked for me:

procedure ShowADSPath(UserName, Password: widestring);
    function CheckObject(APath: String): IDispatch;
    var
      Moniker: IMoniker;
      Eaten: integer;
      BindContext: IBindCtx;
      Dispatch: IDispatch;
    begin
      Result := nil;
      OleCheck(CreateBindCtx(0, BindContext));
      OleCheck(MkParseDisplayName(BindContext, PWideChar(WideString(APath)), 
                                       Eaten, Moniker));
      OleCheck(Moniker.BindToObject(BindContext, nil, IDispatch, Dispatch));

      Result := Dispatch;
    end;
var Group :    IADs;
begin
  try
    if CheckObject('WinNT://Server/Group1,group') <> nil then
         OleCheck(ADsOpenObject('WinNT://Server/Group1,group',
           UserName,
           Password, ADS_SECURE_AUTHENTICATION, IADs, Group));
       if (Group <> nil) and (Group.Class_ = 'Group') then
       begin
         ShowMessage(Group.ADsPath);
         Group.release;  
         Group:= nil;
       end;
  except
     ShowMessage('NOT ACCESSDE');
  end;
end;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top