PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

ActiveDirectory - check if user is member of a group

 
 
Iain
Guest
Posts: n/a
 
      15th Oct 2009
All,

As per subject, tried many examples that none seem to work.

Simply I need to check if the current user is a member of a certain
Active Directory group?

TIA
Iain
 
Reply With Quote
 
 
 
 
Iain
Guest
Posts: n/a
 
      19th Oct 2009
Mark Rae [MVP] wrote:
> "Iain" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> As per subject, tried many examples that none seem to work.
>>
>> Simply I need to check if the current user is a member of a certain
>> Active Directory group?

>
> Firstly, you're in the wrong newsgroup. Please post ActiveDirectory
> questions in the ActiveDirectory newsgroup: microsoft.public.adsi.general
>
> However, the following function returns a List<string> of the groups
> that a user belongs to:
>
> List<string> GetGroupsForUser(string pstrUser)
> {
> List<string> lstGroups = new List<string>();
> using (DirectorySearcher objDS = new
> DirectorySearcher("objectCategory=User"))
> {
> objDS.Filter = "(SAMAccountName=" + pstrUser + ")";
> using (DirectoryEntry objUser = new
> DirectoryEntry(objDS.FindOne().Path))
> {
> PropertyCollection colProperties = objUser.Properties;
> PropertyValueCollection colPropertyValues =
> colProperties["memberOf"];
> foreach (string strGroup in colPropertyValues)
> {
> lstGroups.Add(strGroup.ToLower());
> }
> }
> }
> return lstGroups;
> }
>
> Then, all you have to do is check whether the group you're interested in
> is contained in the generic...
>
> Alternatively, as AD is navigational, not relational, start with the
> group and query AD for its members...
>
>

Sorry about the wrong newsgroup. This code works but only from my local
machine, any other connections non-local return the error message :

"The specified domain either does not exist or could not be contacted."
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      19th Oct 2009
On Oct 19, 2:21*pm, Iain <i...@test.co.uk> wrote:
> Mark Rae [MVP] wrote:
> > "Iain" <i...@test.co.uk> wrote in message
> >news:(E-Mail Removed)...

>
> >> As per subject, tried many examples that none seem to work.

>
> >> Simply I need to check if the current user is a member of a certain
> >> Active Directory group?

>
> > Firstly, you're in the wrong newsgroup. Please post ActiveDirectory
> > questions in the ActiveDirectory newsgroup: microsoft.public.adsi.general

>
> > However, the following function returns a List<string> of the groups
> > that a user belongs to:

>
> > List<string> GetGroupsForUser(string pstrUser)
> > {
> > * *List<string> lstGroups = new List<string>();
> > * *using (DirectorySearcher objDS = new
> > DirectorySearcher("objectCategory=User"))
> > * *{
> > * * * *objDS.Filter = "(SAMAccountName=" + pstrUser + ")";
> > * * * *using (DirectoryEntry objUser = new
> > DirectoryEntry(objDS.FindOne().Path))
> > * * * *{
> > * * * * * *PropertyCollection colProperties = objUser.Properties;
> > * * * * * *PropertyValueCollection colPropertyValues =
> > colProperties["memberOf"];
> > * * * * * *foreach (string strGroup in colPropertyValues)
> > * * * * * *{
> > * * * * * * * *lstGroups.Add(strGroup.ToLower());
> > * * * * * *}
> > * * * *}
> > * *}
> > * *return lstGroups;
> > }

>
> > Then, all you have to do is check whether the group you're interested in
> > is contained in the generic...

>
> > Alternatively, as AD is navigational, not relational, start with the
> > group and query AD for its members...

>
> Sorry about the wrong newsgroup. This code works but only from my local
> machine, any other connections non-local return the error message :
>
> "The specified domain either does not exist or could not be contacted."- Hide quoted text -
>
> - Show quoted text -


is it an ASP.net application?
 
Reply With Quote
 
Iain
Guest
Posts: n/a
 
      19th Oct 2009
Alexey Smirnov wrote:

> is it an ASP.net application?


yes it is.
 
Reply With Quote
 
Iain
Guest
Posts: n/a
 
      19th Oct 2009
>
> There was no mention of remote active directory in your original post...
>
> http://www.codeproject.com/KB/system...thingInAD.aspx and search for
> "Target Specific Domain Controllers or Credentials"
>
>
> Again, active directory questions will likely get a better and faster
> response if you post them in the active directory newsgroup...
>
>

Sorry if my post was not very clear, the machines that I want to connect
via AD are all present within the same domain. The local machine hosting
IIS (essentially my dev box) can connect/query AD, but connecting via
another machine (on the same domain) returns :

"The specified domain either does not exist or could not be contacted."

when trying to access my dev box.
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      19th Oct 2009
On Oct 19, 4:50*pm, Iain <i...@test.co.uk> wrote:
> > There was no mention of remote active directory in your original post....

>
> >http://www.codeproject.com/KB/system...ngInAD.aspxand search for
> > "Target Specific Domain Controllers or Credentials"

>
> > Again, active directory questions will likely get a better and faster
> > response if you post them in the active directory newsgroup...

>
> Sorry if my post was not very clear, the machines that I want to connect
> via AD are all present within the same domain. The local machine hosting
> IIS (essentially my dev box) can connect/query AD, but connecting via
> another machine (on the same domain) returns :
>
> "The specified domain either does not exist or could not be contacted."
>
> when trying to access my dev box.


This probably means that the asp.net account on another machine may
not query AD. Try to look in the event log for more details.

You need to configure your application to be running using Windows
Authentication (go to IIS). Authentication mode must be set to
"Windows" in the web.config file, identity impersonate to true.

To debug your application you can output current account name to see
what the difference is between your dev box and another machine:

Response.Write (User.Identity.Name);
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add ActiveDirectory user to Global Security Group killbill Microsoft C# .NET 1 21st Nov 2006 05:48 AM
Check if user is a member of a domain group =?Utf-8?B?VmlubnkgVmlubg==?= Microsoft ASP .NET 1 27th Sep 2005 10:18 PM
Check if user is a member of a domain group =?Utf-8?B?VmlubnkgVmlubg==?= Microsoft C# .NET 0 27th Sep 2005 08:02 PM
Check group member ship or a user Sameh Ahmed Microsoft VB .NET 9 21st Feb 2005 10:46 AM
Using Net Group or GPO to add member to Power User Group OJ Microsoft Windows 2000 Active Directory 2 24th Sep 2004 05:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:56 PM.