Check if user is a member of a domain group

G

Guest

I am writing a C#/asp.net app.I have a requirement to check if the user is
member of specific Domain group,how can this be accomplished?
I tried the following and it always returns false ,even if the user is part
of the group.
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if(wp.IsInRole(@"Domain\group"))
{
MessageBox.Show("member",wp.Identity.Name);
}
else
{
MessageBox.Show("not menber",wp.Identity.Name);
}

TIA,
Vinny
 
M

Mark Rae

Try this:

using System.DirectoryServices;

public bool IsUserInGroup(string pstrDomain, string pstrUser, string
pstrGroup)
{
try
{
DirectoryEntry objADEntry = new DirectoryEntry("WinNT://" + pstrDomain +
",domain");
DirectoryEntry objUser = objADEntry.Children.Find(pstrUser, "user");
DirectoryEntry objGroup = objADEntry.Children.Find(pstrGroup, "group");
return (bool) objGroup.Invoke("IsMember", new object[]
{objUser.Path.ToString()});
}
catch (Exception)
{
throw;
}
}

MessageBox.Show("not menber",wp.Identity.Name);
MessageBox.Show won't help you in ASP.NET... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top