Public Shared Function IsUserInGroup(pstrDomain As String, pstrUser As String, pstrGroup
As String) As Boolean
Dim objADEntry As DirectoryEntry = Nothing
Dim objUser As DirectoryEntry = Nothing
Dim objGroup As DirectoryEntry = Nothing
Try
objADEntry = New DirectoryEntry("WinNT://" + pstrDomain + ",domain")
objUser = objADEntry.Children.Find(pstrUser, "user")
objGroup = objADEntry.Children.Find(pstrGroup, "group")
Return CBool(objGroup.Invoke("IsMember", New Object() {objUser.Path.ToString()}))
Catch Else
Throw
Finally
If Not (objGroup Is Nothing) Then
objGroup.Close()
objGroup.Dispose()
objGroup = Nothing
End If
If Not (objUser Is Nothing) Then
objUser.Close()
objUser.Dispose()
objUser = Nothing
End If
If Not (objADEntry Is Nothing) Then
objADEntry.Close()
objADEntry.Dispose()
objADEntry = Nothing
End If
End Try
End Function 'IsUserInGroup
Courtesy of "C# To VB .NET Source Code Converter":
http://www.eggheadcafe.com/articles/cstovbweb/converter.aspx
It took about 3 seconds to get that code converted... ;-)
You might want to check it for errors,
but at least the grunt work is done for you.
Thanks, I will trying that after the "translation" to VB.
Mark Rae said:
I have done that and it correctly identifies me.
OK.
However, the problem still remains of finding out if I belong to the said
group in Windows.
Might it be simply that the ASPNET account doesn't have sufficient
permissions to query the ActiveDirectory...?
Below is an extremely simple but very effective C# method I use for
determining whether a given user is in a given group - it shouldn't be too
difficult to convert it into VB.NET - you'll need to reference the
System.DirectoryServices namespace...
public static bool IsUserInGroup(string pstrDomain, string pstrUser, string
pstrGroup)
{
DirectoryEntry objADEntry = null;
DirectoryEntry objUser = null;
DirectoryEntry objGroup = null;
try
{
objADEntry = new DirectoryEntry("WinNT://" + pstrDomain + ",domain");
objUser = objADEntry.Children.Find(pstrUser, "user");
objGroup = objADEntry.Children.Find(pstrGroup, "group");
return (bool) objGroup.Invoke("IsMember", new object[]
{objUser.Path.ToString()});
}
catch (Exception)
{
throw;
}
finally
{
if (objGroup != null)
{
objGroup.Close();
objGroup.Dispose();
objGroup = null;
}
if (objUser != null)
{
objUser.Close();
objUser.Dispose();
objUser = null;
}
if (objADEntry != null)
{
objADEntry.Close();
objADEntry.Dispose();
objADEntry = null;
}
}
}