robtroller
asked on
Is UserName A Member Of An AD Security Group?
I am using the code below to check if the user logged onto the PC is a member of a specific AD Security Group. Now I need to perform the same task with a username the user types into a web page and I capture in a string variable (Becaise the user on the web page may not be the user logged into the PC). I'm not sure how to edit the GetGroups Function to allow the username to be passed from a string.
public string IsInGroup(string strGroup)
{
bool _Test = false;
string strTest = "False";
// collect the user domain and identity
string[] arr =
System.Web.HttpContext.Cur rent.Reque st.
LogonUserIdentity.Name.Spl it('\\');
ArrayList al = new ArrayList();
al = GetGroups();
// check to see if the user belongs
// to a specific group and create
// a list of all of the user's groups
foreach (string s in al)
{
// check to see if the user
// belongs to a specific group
if (s == strGroup)
{
_Test = true;
//return strTest = "True";
}
}
if (_Test == false)
{
return strTest = "False";
}
else {
return strTest = "True";
}
}
public ArrayList GetGroups()
{
ArrayList groups = new ArrayList();
foreach (System.Security.Principal .IdentityR eference group in
System.Web.HttpContext.Cur rent.Reque st.LogonUs erIdentity .Groups)
{
groups.Add(group.Translate (typeof
(System.Security.Principal .NTAccount )).ToStrin g());
}
return groups;
}
public string IsInGroup(string strGroup)
{
bool _Test = false;
string strTest = "False";
// collect the user domain and identity
string[] arr =
System.Web.HttpContext.Cur
LogonUserIdentity.Name.Spl
ArrayList al = new ArrayList();
al = GetGroups();
// check to see if the user belongs
// to a specific group and create
// a list of all of the user's groups
foreach (string s in al)
{
// check to see if the user
// belongs to a specific group
if (s == strGroup)
{
_Test = true;
//return strTest = "True";
}
}
if (_Test == false)
{
return strTest = "False";
}
else {
return strTest = "True";
}
}
public ArrayList GetGroups()
{
ArrayList groups = new ArrayList();
foreach (System.Security.Principal
System.Web.HttpContext.Cur
{
groups.Add(group.Translate
(System.Security.Principal
}
return groups;
}
ASKER
I'm not having any luck converting this to C#. In a nutshell, I wan to query AD for user Security Groups based on a string tht contains the username instead of using the System.Security.Principal. IdentityRe ference. Seems like it shouldn't be an unusual thing to do as we can pass a username to the DirectoryEntry object to validate username & password. I'm really stuck on this. Any C# assistance would be greately appreciated.
the second example I gave you is actually c#
ASKER
OK, trying again. I am missint a system reference do you know which one?
ASKER
Current references are :
using System;
using System.Security;
using System.Collections;
using System.Collections.Generic ;
using System.Security.Principal;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;
using System;
using System.Security;
using System.Collections;
using System.Collections.Generic
using System.Security.Principal;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices;
I have knocked up a sample for you.
In your applicaiton, remember that you need to add a reference to System.DirectoryServices.A ccountMana gement (in System.DirectoryServices.A ccountMana gement.dll )
DemoActiveDirectory.zip
In your applicaiton, remember that you need to add a reference to System.DirectoryServices.A
DemoActiveDirectory.zip
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is how I resolved the issue
Open in new window
Open in new window