Home All Groups Group Topic Archive Search About

Authorization against AD using MC++

Author
2 Aug 2005 6:52 PM
albier
I wrote a code with C# to authorize users against AD. Here is the code:

ArrayList usersList = new ArrayList();
object groups = user.Invoke("Groups",null);
foreach (object group in (IEnumerable)groups)  
{
    DirectoryEntry groupEntry  = new DirectoryEntry(group);
    usersList.Add(groupEntry.Name);
}

This code is working perfectly. I'm having problems writing the same code
using Managed C++. A similar code would be:

ArrayList* usersList = new ArrayList();
System::Object* groups = de->Invoke("Groups", NULL);       
while (groups != NULL)
{
                DirectoryEntry* groupEntry  = new DirectoryEntry(groups);
    usersList->Add(groupEntry->Name);
    groups++;
}

This code doesn't work since pointers in MC++ cannot be incremented and I
cannot use them to refer to the variable they are pointing to. Since these
pointers are __gc type (Garbage Collected), I cannot say *groups to refer to
the value the pointer groups is pointing to. I also cannot say groups++ to
increment the pointer so that groups would point to the next object in the
list of objects. Please notice that the Invoke "Group" function would return
a list of objects and I need to loop over this list. Do you have a solution
for this problem? Do you have any other idea of how to implement the C# code
above?
Thanks for your help,
Albier Michaiel

Author
2 Aug 2005 7:16 PM
Joe Kaplan (MVP - ADSI)
You should probably just call the IEnumerable::GetEnumerator method and then
use its MoveNext and Current members to enumerate the group collection.
That is all foreach does under the hood.

Note that the actual approach you are using may not work for nested group
membership or the user's primary group.  It is a better idea to do this kind
of authorization based on the user's logon token and the token_groups
structure inside of it.

Joe K.

Show quoteHide quote
"albier" <alb***@discussions.microsoft.com> wrote in message
news:7067232E-4D89-42DC-B7CA-7A58A72587B9@microsoft.com...
>I wrote a code with C# to authorize users against AD. Here is the code:
>
> ArrayList usersList = new ArrayList();
> object groups = user.Invoke("Groups",null);
> foreach (object group in (IEnumerable)groups)
> {
> DirectoryEntry groupEntry  = new DirectoryEntry(group);
> usersList.Add(groupEntry.Name);
> }
>
> This code is working perfectly. I'm having problems writing the same code
> using Managed C++. A similar code would be:
>
> ArrayList* usersList = new ArrayList();
> System::Object* groups = de->Invoke("Groups", NULL);
> while (groups != NULL)
> {
>                DirectoryEntry* groupEntry  = new DirectoryEntry(groups);
> usersList->Add(groupEntry->Name);
> groups++;
> }
>
> This code doesn't work since pointers in MC++ cannot be incremented and I
> cannot use them to refer to the variable they are pointing to. Since these
> pointers are __gc type (Garbage Collected), I cannot say *groups to refer
> to
> the value the pointer groups is pointing to. I also cannot say groups++ to
> increment the pointer so that groups would point to the next object in the
> list of objects. Please notice that the Invoke "Group" function would
> return
> a list of objects and I need to loop over this list. Do you have a
> solution
> for this problem? Do you have any other idea of how to implement the C#
> code
> above?
> Thanks for your help,
> Albier Michaiel
>
>
Author
2 Aug 2005 9:07 PM
albier
Joe,
Thanks for the pointers but I still don't know how to use the
IEnumerable::GetEnumerator to be able to loop over the list of objects I
have. I have tried the following code but got an error in for trying to
type-cast groups.

ArrayList* usersList = new ArrayList();
System::Object* groups = de->Invoke("Groups", NULL);

while (((IEnumerator*)groups)->MoveNext())
{
DirectoryEntry* groupEntry  = new
DirectoryEntry(((IEnumerator*)groups)->Current);
usersList->Add(groupEntry->Name);
}

It would be great if you can just show me how to use the
IEnumerable::GetEnumerator in order to loop over the list of objects I have
(groups*). Thanks again for your time,
Albier



Show quoteHide quote
"Joe Kaplan (MVP - ADSI)" wrote:

> You should probably just call the IEnumerable::GetEnumerator method and then
> use its MoveNext and Current members to enumerate the group collection.
> That is all foreach does under the hood.
>
> Note that the actual approach you are using may not work for nested group
> membership or the user's primary group.  It is a better idea to do this kind
> of authorization based on the user's logon token and the token_groups
> structure inside of it.
>
> Joe K.
>
> "albier" <alb***@discussions.microsoft.com> wrote in message
> news:7067232E-4D89-42DC-B7CA-7A58A72587B9@microsoft.com...
> >I wrote a code with C# to authorize users against AD. Here is the code:
> >
> > ArrayList usersList = new ArrayList();
> > object groups = user.Invoke("Groups",null);
> > foreach (object group in (IEnumerable)groups)
> > {
> > DirectoryEntry groupEntry  = new DirectoryEntry(group);
> > usersList.Add(groupEntry.Name);
> > }
> >
> > This code is working perfectly. I'm having problems writing the same code
> > using Managed C++. A similar code would be:
> >
> > ArrayList* usersList = new ArrayList();
> > System::Object* groups = de->Invoke("Groups", NULL);
> > while (groups != NULL)
> > {
> >                DirectoryEntry* groupEntry  = new DirectoryEntry(groups);
> > usersList->Add(groupEntry->Name);
> > groups++;
> > }
> >
> > This code doesn't work since pointers in MC++ cannot be incremented and I
> > cannot use them to refer to the variable they are pointing to. Since these
> > pointers are __gc type (Garbage Collected), I cannot say *groups to refer
> > to
> > the value the pointer groups is pointing to. I also cannot say groups++ to
> > increment the pointer so that groups would point to the next object in the
> > list of objects. Please notice that the Invoke "Group" function would
> > return
> > a list of objects and I need to loop over this list. Do you have a
> > solution
> > for this problem? Do you have any other idea of how to implement the C#
> > code
> > above?
> > Thanks for your help,
> > Albier Michaiel
> >
> >
>
>
>
Author
2 Aug 2005 9:40 PM
albier
I also tried the following code but got more or less the same problem:

ArrayList* usersList = new ArrayList();
Object* groups = de->Invoke("Groups", NULL);

while (((IEnumerable*)groups)->GetEnumerator()->MoveNext())
{
    DirectoryEntry* groupEntry  = new
DirectoryEntry(((IEnumerable*)groups)->GetEnumerator()->Current);
    usersList->Add(groupEntry->Name);
}

Your help is appreciated.
Albier

Show quoteHide quote
"albier" wrote:

> Joe,
> Thanks for the pointers but I still don't know how to use the
> IEnumerable::GetEnumerator to be able to loop over the list of objects I
> have. I have tried the following code but got an error in for trying to
> type-cast groups.
>
> ArrayList* usersList = new ArrayList();
> System::Object* groups = de->Invoke("Groups", NULL);
>                
> while (((IEnumerator*)groups)->MoveNext())
> {
> DirectoryEntry* groupEntry  = new
> DirectoryEntry(((IEnumerator*)groups)->Current);
> usersList->Add(groupEntry->Name);
> }
>
> It would be great if you can just show me how to use the
> IEnumerable::GetEnumerator in order to loop over the list of objects I have
> (groups*). Thanks again for your time,
> Albier
>
>
>
> "Joe Kaplan (MVP - ADSI)" wrote:
>
> > You should probably just call the IEnumerable::GetEnumerator method and then
> > use its MoveNext and Current members to enumerate the group collection.
> > That is all foreach does under the hood.
> >
> > Note that the actual approach you are using may not work for nested group
> > membership or the user's primary group.  It is a better idea to do this kind
> > of authorization based on the user's logon token and the token_groups
> > structure inside of it.
> >
> > Joe K.
> >
> > "albier" <alb***@discussions.microsoft.com> wrote in message
> > news:7067232E-4D89-42DC-B7CA-7A58A72587B9@microsoft.com...
> > >I wrote a code with C# to authorize users against AD. Here is the code:
> > >
> > > ArrayList usersList = new ArrayList();
> > > object groups = user.Invoke("Groups",null);
> > > foreach (object group in (IEnumerable)groups)
> > > {
> > > DirectoryEntry groupEntry  = new DirectoryEntry(group);
> > > usersList.Add(groupEntry.Name);
> > > }
> > >
> > > This code is working perfectly. I'm having problems writing the same code
> > > using Managed C++. A similar code would be:
> > >
> > > ArrayList* usersList = new ArrayList();
> > > System::Object* groups = de->Invoke("Groups", NULL);
> > > while (groups != NULL)
> > > {
> > >                DirectoryEntry* groupEntry  = new DirectoryEntry(groups);
> > > usersList->Add(groupEntry->Name);
> > > groups++;
> > > }
> > >
> > > This code doesn't work since pointers in MC++ cannot be incremented and I
> > > cannot use them to refer to the variable they are pointing to. Since these
> > > pointers are __gc type (Garbage Collected), I cannot say *groups to refer
> > > to
> > > the value the pointer groups is pointing to. I also cannot say groups++ to
> > > increment the pointer so that groups would point to the next object in the
> > > list of objects. Please notice that the Invoke "Group" function would
> > > return
> > > a list of objects and I need to loop over this list. Do you have a
> > > solution
> > > for this problem? Do you have any other idea of how to implement the C#
> > > code
> > > above?
> > > Thanks for your help,
> > > Albier Michaiel
> > >
> > >
> >
> >
> >
Author
3 Aug 2005 12:34 AM
Joe Kaplan (MVP - ADSI)
I believe you need to call GetEnumerator once, not in the loop body, and
then enumerate instance returned by it.  I'm not really an MC++ guy though,
I'm the directory services programming guy, so someone else with more
experience here might be able to help.

Joe K.

Show quoteHide quote
"albier" <alb***@discussions.microsoft.com> wrote in message
news:7ACDD09C-CEBF-4FB3-9A2C-3ABCA0E2CAFC@microsoft.com...
>I also tried the following code but got more or less the same problem:
>
> ArrayList* usersList = new ArrayList();
> Object* groups = de->Invoke("Groups", NULL);
>
> while (((IEnumerable*)groups)->GetEnumerator()->MoveNext())
> {
> DirectoryEntry* groupEntry  = new
> DirectoryEntry(((IEnumerable*)groups)->GetEnumerator()->Current);
> usersList->Add(groupEntry->Name);
> }
>
> Your help is appreciated.
> Albier
>
> "albier" wrote:
>
>> Joe,
>> Thanks for the pointers but I still don't know how to use the
>> IEnumerable::GetEnumerator to be able to loop over the list of objects I
>> have. I have tried the following code but got an error in for trying to
>> type-cast groups.
>>
>> ArrayList* usersList = new ArrayList();
>> System::Object* groups = de->Invoke("Groups", NULL);
>>
>> while (((IEnumerator*)groups)->MoveNext())
>> {
>> DirectoryEntry* groupEntry  = new
>> DirectoryEntry(((IEnumerator*)groups)->Current);
>> usersList->Add(groupEntry->Name);
>> }
>>
>> It would be great if you can just show me how to use the
>> IEnumerable::GetEnumerator in order to loop over the list of objects I
>> have
>> (groups*). Thanks again for your time,
>> Albier
>>
>>
>>
>> "Joe Kaplan (MVP - ADSI)" wrote:
>>
>> > You should probably just call the IEnumerable::GetEnumerator method and
>> > then
>> > use its MoveNext and Current members to enumerate the group collection.
>> > That is all foreach does under the hood.
>> >
>> > Note that the actual approach you are using may not work for nested
>> > group
>> > membership or the user's primary group.  It is a better idea to do this
>> > kind
>> > of authorization based on the user's logon token and the token_groups
>> > structure inside of it.
>> >
>> > Joe K.
>> >
>> > "albier" <alb***@discussions.microsoft.com> wrote in message
>> > news:7067232E-4D89-42DC-B7CA-7A58A72587B9@microsoft.com...
>> > >I wrote a code with C# to authorize users against AD. Here is the
>> > >code:
>> > >
>> > > ArrayList usersList = new ArrayList();
>> > > object groups = user.Invoke("Groups",null);
>> > > foreach (object group in (IEnumerable)groups)
>> > > {
>> > > DirectoryEntry groupEntry  = new DirectoryEntry(group);
>> > > usersList.Add(groupEntry.Name);
>> > > }
>> > >
>> > > This code is working perfectly. I'm having problems writing the same
>> > > code
>> > > using Managed C++. A similar code would be:
>> > >
>> > > ArrayList* usersList = new ArrayList();
>> > > System::Object* groups = de->Invoke("Groups", NULL);
>> > > while (groups != NULL)
>> > > {
>> > >                DirectoryEntry* groupEntry  = new
>> > > DirectoryEntry(groups);
>> > > usersList->Add(groupEntry->Name);
>> > > groups++;
>> > > }
>> > >
>> > > This code doesn't work since pointers in MC++ cannot be incremented
>> > > and I
>> > > cannot use them to refer to the variable they are pointing to. Since
>> > > these
>> > > pointers are __gc type (Garbage Collected), I cannot say *groups to
>> > > refer
>> > > to
>> > > the value the pointer groups is pointing to. I also cannot say
>> > > groups++ to
>> > > increment the pointer so that groups would point to the next object
>> > > in the
>> > > list of objects. Please notice that the Invoke "Group" function would
>> > > return
>> > > a list of objects and I need to loop over this list. Do you have a
>> > > solution
>> > > for this problem? Do you have any other idea of how to implement the
>> > > C#
>> > > code
>> > > above?
>> > > Thanks for your help,
>> > > Albier Michaiel
>> > >
>> > >
>> >
>> >
>> >