N
Natalia DeBow
Hi,
In my app, I need to check if the current user is a member of the
Administrators group in order to allow/deny some action.
I read some posts from Willy Denoyette (VIP) where he actually posted some
code how to get the group membership for a given user.
I am using System.DirectoryServices namespace provided by .NET Framework 1.1
with the implementation language being C#.
Here is the code that is generating a null pointer exception to be thrown:
using System.DirectoryServices; ///<remarks> User Account Info </remarks>
using System.Runtime.InteropServices;
using activedsnet;
..........
private bool verifyCurrentUserAccount()
{
IADsMembers MembersCollection = null;
DirectoryEntry _groupEntry = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
try
{
// call native method "members" on the IADsGroup COM interface exposed by
activeds.dll
IADsGroup gr = _groupEntry.NativeObject as IADsGroup;
MembersCollection = gr.Members(); *** --> gr is NULL here, thus
causing a null pointer exception to be thrown. ***
// or call Invoke on the DirectoryEntry object passing the Method to call as
arg.
// cast the retruned object to IADsMembers
// MembersCollection = _groupEntry.Invoke("Members") as IADsMembers;
object[] filter = {"user"};
MembersCollection.Filter = filter;
// enumerate members of collection object that supports the IADsMembers
interface
// ADSI provider doesn't support count property!!
try
{
foreach (IADsUser member in MembersCollection)
{
System.Console.WriteLine("[{0}]", member.Name);
ListUserProp(member.ADsPath);
}
}
catch (Exception e)
{
System.Console.WriteLine("Error: {0}",e.Message);
}
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
finally
{
_groupEntry.Dispose();
}
return true;
}
private static void ListUserProp(string dirPath)
{
DirectoryEntry userEntry = null;
try
{
userEntry = new DirectoryEntry(dirPath);
PropertyCollection pcoll = userEntry.Properties;
foreach(string sc in pcoll.PropertyNames)
System.Console.WriteLine("\t" + sc + "\t" + pcoll[sc].Value);
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
finally
{
userEntry.Dispose();
}
}
I added a reference to activedsnet.dll for the project, so I don't think
that's the issue.
It seems like my activedsnet.dll is not working, but I verified if all the
interfaces were generated. I had to run TlbImp.exe tool on activeds.tlb to
generate my activedsnet.dll. The TlbImp generated a lot of warnings, but no
errors. I am not sure if that's the problem or if there is some other issue
that causes my code to blow up.
Any comments/suggestions are greatly appreciated.
Thanks,
Natalia
In my app, I need to check if the current user is a member of the
Administrators group in order to allow/deny some action.
I read some posts from Willy Denoyette (VIP) where he actually posted some
code how to get the group membership for a given user.
I am using System.DirectoryServices namespace provided by .NET Framework 1.1
with the implementation language being C#.
Here is the code that is generating a null pointer exception to be thrown:
using System.DirectoryServices; ///<remarks> User Account Info </remarks>
using System.Runtime.InteropServices;
using activedsnet;
..........
private bool verifyCurrentUserAccount()
{
IADsMembers MembersCollection = null;
DirectoryEntry _groupEntry = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
try
{
// call native method "members" on the IADsGroup COM interface exposed by
activeds.dll
IADsGroup gr = _groupEntry.NativeObject as IADsGroup;
MembersCollection = gr.Members(); *** --> gr is NULL here, thus
causing a null pointer exception to be thrown. ***
// or call Invoke on the DirectoryEntry object passing the Method to call as
arg.
// cast the retruned object to IADsMembers
// MembersCollection = _groupEntry.Invoke("Members") as IADsMembers;
object[] filter = {"user"};
MembersCollection.Filter = filter;
// enumerate members of collection object that supports the IADsMembers
interface
// ADSI provider doesn't support count property!!
try
{
foreach (IADsUser member in MembersCollection)
{
System.Console.WriteLine("[{0}]", member.Name);
ListUserProp(member.ADsPath);
}
}
catch (Exception e)
{
System.Console.WriteLine("Error: {0}",e.Message);
}
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
finally
{
_groupEntry.Dispose();
}
return true;
}
private static void ListUserProp(string dirPath)
{
DirectoryEntry userEntry = null;
try
{
userEntry = new DirectoryEntry(dirPath);
PropertyCollection pcoll = userEntry.Properties;
foreach(string sc in pcoll.PropertyNames)
System.Console.WriteLine("\t" + sc + "\t" + pcoll[sc].Value);
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
finally
{
userEntry.Dispose();
}
}
I added a reference to activedsnet.dll for the project, so I don't think
that's the issue.
It seems like my activedsnet.dll is not working, but I verified if all the
interfaces were generated. I had to run TlbImp.exe tool on activeds.tlb to
generate my activedsnet.dll. The TlbImp generated a lot of warnings, but no
errors. I am not sure if that's the problem or if there is some other issue
that causes my code to blow up.
Any comments/suggestions are greatly appreciated.
Thanks,
Natalia