Find if user belongs to group
up vote
1
down vote
favorite
I want to find if the user belongs to an AD group. Can you advise how I can add that functionality using the following code?
I ask the user to enter their username and password (through a form), so not using the windows credentials. With the below code I am able to validate the user, by passing the username, and password. How can I build on the code to check if user exists in the AD Group. Is there another way to do this? Please advice
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
try
SearchResult adsSearchResult = adsSearcher.FindOne();
context.Session.Timeout = 2;
context.Session["ValidatedLoginID"] = userid;
user.Verified = true;
adsEntry.Close();
catch ( Exception ex )
// Failed to authenticate. Most likely it is caused by unknown user
// id or bad strPassword.
user.error = ex.Message;
adsEntry.Close();
c# active-directory
add a comment |
up vote
1
down vote
favorite
I want to find if the user belongs to an AD group. Can you advise how I can add that functionality using the following code?
I ask the user to enter their username and password (through a form), so not using the windows credentials. With the below code I am able to validate the user, by passing the username, and password. How can I build on the code to check if user exists in the AD Group. Is there another way to do this? Please advice
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
try
SearchResult adsSearchResult = adsSearcher.FindOne();
context.Session.Timeout = 2;
context.Session["ValidatedLoginID"] = userid;
user.Verified = true;
adsEntry.Close();
catch ( Exception ex )
// Failed to authenticate. Most likely it is caused by unknown user
// id or bad strPassword.
user.error = ex.Message;
adsEntry.Close();
c# active-directory
Possible duplicate of How to check if a user belongs to an AD group?
– freedomn-m
Nov 9 at 15:20
I talked a bit about that in an article I wrote here: gabescode.com/active-directory/2018/09/13/…
– Gabriel Luci
Nov 9 at 15:27
Thanks Gabriel, Sorry I just updated my question. User fills a form to enter credentials. so I cant use the windows authenication method. I will need to use a different way if you know what I mean
– edcoder
Nov 9 at 15:40
That's fine. My article discussed doing the same withDirectoryEntry. But you will have to search for their user account first. There are lots of examples online of finding a user by username withDirectorySearcher.
– Gabriel Luci
Nov 9 at 16:00
Thank You Gabriel..will update how I go about it. Your articles are really good.
– edcoder
Nov 10 at 8:56
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to find if the user belongs to an AD group. Can you advise how I can add that functionality using the following code?
I ask the user to enter their username and password (through a form), so not using the windows credentials. With the below code I am able to validate the user, by passing the username, and password. How can I build on the code to check if user exists in the AD Group. Is there another way to do this? Please advice
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
try
SearchResult adsSearchResult = adsSearcher.FindOne();
context.Session.Timeout = 2;
context.Session["ValidatedLoginID"] = userid;
user.Verified = true;
adsEntry.Close();
catch ( Exception ex )
// Failed to authenticate. Most likely it is caused by unknown user
// id or bad strPassword.
user.error = ex.Message;
adsEntry.Close();
c# active-directory
I want to find if the user belongs to an AD group. Can you advise how I can add that functionality using the following code?
I ask the user to enter their username and password (through a form), so not using the windows credentials. With the below code I am able to validate the user, by passing the username, and password. How can I build on the code to check if user exists in the AD Group. Is there another way to do this? Please advice
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
try
SearchResult adsSearchResult = adsSearcher.FindOne();
context.Session.Timeout = 2;
context.Session["ValidatedLoginID"] = userid;
user.Verified = true;
adsEntry.Close();
catch ( Exception ex )
// Failed to authenticate. Most likely it is caused by unknown user
// id or bad strPassword.
user.error = ex.Message;
adsEntry.Close();
c# active-directory
c# active-directory
edited Nov 9 at 15:38
asked Nov 9 at 14:00
edcoder
16411
16411
Possible duplicate of How to check if a user belongs to an AD group?
– freedomn-m
Nov 9 at 15:20
I talked a bit about that in an article I wrote here: gabescode.com/active-directory/2018/09/13/…
– Gabriel Luci
Nov 9 at 15:27
Thanks Gabriel, Sorry I just updated my question. User fills a form to enter credentials. so I cant use the windows authenication method. I will need to use a different way if you know what I mean
– edcoder
Nov 9 at 15:40
That's fine. My article discussed doing the same withDirectoryEntry. But you will have to search for their user account first. There are lots of examples online of finding a user by username withDirectorySearcher.
– Gabriel Luci
Nov 9 at 16:00
Thank You Gabriel..will update how I go about it. Your articles are really good.
– edcoder
Nov 10 at 8:56
add a comment |
Possible duplicate of How to check if a user belongs to an AD group?
– freedomn-m
Nov 9 at 15:20
I talked a bit about that in an article I wrote here: gabescode.com/active-directory/2018/09/13/…
– Gabriel Luci
Nov 9 at 15:27
Thanks Gabriel, Sorry I just updated my question. User fills a form to enter credentials. so I cant use the windows authenication method. I will need to use a different way if you know what I mean
– edcoder
Nov 9 at 15:40
That's fine. My article discussed doing the same withDirectoryEntry. But you will have to search for their user account first. There are lots of examples online of finding a user by username withDirectorySearcher.
– Gabriel Luci
Nov 9 at 16:00
Thank You Gabriel..will update how I go about it. Your articles are really good.
– edcoder
Nov 10 at 8:56
Possible duplicate of How to check if a user belongs to an AD group?
– freedomn-m
Nov 9 at 15:20
Possible duplicate of How to check if a user belongs to an AD group?
– freedomn-m
Nov 9 at 15:20
I talked a bit about that in an article I wrote here: gabescode.com/active-directory/2018/09/13/…
– Gabriel Luci
Nov 9 at 15:27
I talked a bit about that in an article I wrote here: gabescode.com/active-directory/2018/09/13/…
– Gabriel Luci
Nov 9 at 15:27
Thanks Gabriel, Sorry I just updated my question. User fills a form to enter credentials. so I cant use the windows authenication method. I will need to use a different way if you know what I mean
– edcoder
Nov 9 at 15:40
Thanks Gabriel, Sorry I just updated my question. User fills a form to enter credentials. so I cant use the windows authenication method. I will need to use a different way if you know what I mean
– edcoder
Nov 9 at 15:40
That's fine. My article discussed doing the same with
DirectoryEntry. But you will have to search for their user account first. There are lots of examples online of finding a user by username with DirectorySearcher.– Gabriel Luci
Nov 9 at 16:00
That's fine. My article discussed doing the same with
DirectoryEntry. But you will have to search for their user account first. There are lots of examples online of finding a user by username with DirectorySearcher.– Gabriel Luci
Nov 9 at 16:00
Thank You Gabriel..will update how I go about it. Your articles are really good.
– edcoder
Nov 10 at 8:56
Thank You Gabriel..will update how I go about it. Your articles are really good.
– edcoder
Nov 10 at 8:56
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
You can use the below code:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME");
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
if(user != null)
// check if user is member of that group
if (user.IsMemberOf(group))
// do something.....
Also look at: How to check if a user belongs to an AD group?
Hi Aman, thank you so much, but I need to validate the users password. I dont want to allow them to do something if password does not match. Can you advice how I can do that?
– edcoder
Nov 9 at 14:36
@edcoder In your post you had mentioned that you were able to authenticate user and need to check if user belongs to the group. Thats why I shared above code. You might need to update your post and correct this. You would also need to provide details against what do you want your user to authenticate. Do you have some database or if its a windows login etc.
– aman
Nov 9 at 14:43
@edcoder your post clearly states that you want to check if the user is in an AD group. There is no mention of checking passwords. Please ask a new question.
– freedomn-m
Nov 9 at 15:19
@aman did you just copy code from an existing answer? You should vote close as a duplicate, not copy someone else's work (even if you provided a link back).
– freedomn-m
Nov 9 at 15:20
1
With the code I posted above I believe you can do something like: PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Company.com", "DC=SomeDC,DC=COM", username, Password); where you pass your username and password. Rest code remains same
– aman
Nov 9 at 15:55
|
show 3 more comments
up vote
0
down vote
Here is how I solved this :
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
adsSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + userid + "))";
try
SearchResult adsSearchResult = adsSearcher.FindOne();
string propertyName = "memberOf";
ResultPropertyValueCollection rpvcResult = adsSearchResult.Properties[propertyName];
foreach (Object PropertyValue in rpvcResult)
if (PropertyValue.ToString() == "Group Name")
user.Verified = true;
user.FullName = GetFullName(userid);
adsEntry.Close();
else
user.Verified = false;
user.error = "You do not belong to the Group so you cannot do this function";
catch (Exception ex)
user.error = "Please check your username and password credentials";
adsEntry.Close();
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use the below code:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME");
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
if(user != null)
// check if user is member of that group
if (user.IsMemberOf(group))
// do something.....
Also look at: How to check if a user belongs to an AD group?
Hi Aman, thank you so much, but I need to validate the users password. I dont want to allow them to do something if password does not match. Can you advice how I can do that?
– edcoder
Nov 9 at 14:36
@edcoder In your post you had mentioned that you were able to authenticate user and need to check if user belongs to the group. Thats why I shared above code. You might need to update your post and correct this. You would also need to provide details against what do you want your user to authenticate. Do you have some database or if its a windows login etc.
– aman
Nov 9 at 14:43
@edcoder your post clearly states that you want to check if the user is in an AD group. There is no mention of checking passwords. Please ask a new question.
– freedomn-m
Nov 9 at 15:19
@aman did you just copy code from an existing answer? You should vote close as a duplicate, not copy someone else's work (even if you provided a link back).
– freedomn-m
Nov 9 at 15:20
1
With the code I posted above I believe you can do something like: PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Company.com", "DC=SomeDC,DC=COM", username, Password); where you pass your username and password. Rest code remains same
– aman
Nov 9 at 15:55
|
show 3 more comments
up vote
0
down vote
You can use the below code:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME");
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
if(user != null)
// check if user is member of that group
if (user.IsMemberOf(group))
// do something.....
Also look at: How to check if a user belongs to an AD group?
Hi Aman, thank you so much, but I need to validate the users password. I dont want to allow them to do something if password does not match. Can you advice how I can do that?
– edcoder
Nov 9 at 14:36
@edcoder In your post you had mentioned that you were able to authenticate user and need to check if user belongs to the group. Thats why I shared above code. You might need to update your post and correct this. You would also need to provide details against what do you want your user to authenticate. Do you have some database or if its a windows login etc.
– aman
Nov 9 at 14:43
@edcoder your post clearly states that you want to check if the user is in an AD group. There is no mention of checking passwords. Please ask a new question.
– freedomn-m
Nov 9 at 15:19
@aman did you just copy code from an existing answer? You should vote close as a duplicate, not copy someone else's work (even if you provided a link back).
– freedomn-m
Nov 9 at 15:20
1
With the code I posted above I believe you can do something like: PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Company.com", "DC=SomeDC,DC=COM", username, Password); where you pass your username and password. Rest code remains same
– aman
Nov 9 at 15:55
|
show 3 more comments
up vote
0
down vote
up vote
0
down vote
You can use the below code:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME");
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
if(user != null)
// check if user is member of that group
if (user.IsMemberOf(group))
// do something.....
Also look at: How to check if a user belongs to an AD group?
You can use the below code:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME");
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
if(user != null)
// check if user is member of that group
if (user.IsMemberOf(group))
// do something.....
Also look at: How to check if a user belongs to an AD group?
answered Nov 9 at 14:25
aman
4851516
4851516
Hi Aman, thank you so much, but I need to validate the users password. I dont want to allow them to do something if password does not match. Can you advice how I can do that?
– edcoder
Nov 9 at 14:36
@edcoder In your post you had mentioned that you were able to authenticate user and need to check if user belongs to the group. Thats why I shared above code. You might need to update your post and correct this. You would also need to provide details against what do you want your user to authenticate. Do you have some database or if its a windows login etc.
– aman
Nov 9 at 14:43
@edcoder your post clearly states that you want to check if the user is in an AD group. There is no mention of checking passwords. Please ask a new question.
– freedomn-m
Nov 9 at 15:19
@aman did you just copy code from an existing answer? You should vote close as a duplicate, not copy someone else's work (even if you provided a link back).
– freedomn-m
Nov 9 at 15:20
1
With the code I posted above I believe you can do something like: PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Company.com", "DC=SomeDC,DC=COM", username, Password); where you pass your username and password. Rest code remains same
– aman
Nov 9 at 15:55
|
show 3 more comments
Hi Aman, thank you so much, but I need to validate the users password. I dont want to allow them to do something if password does not match. Can you advice how I can do that?
– edcoder
Nov 9 at 14:36
@edcoder In your post you had mentioned that you were able to authenticate user and need to check if user belongs to the group. Thats why I shared above code. You might need to update your post and correct this. You would also need to provide details against what do you want your user to authenticate. Do you have some database or if its a windows login etc.
– aman
Nov 9 at 14:43
@edcoder your post clearly states that you want to check if the user is in an AD group. There is no mention of checking passwords. Please ask a new question.
– freedomn-m
Nov 9 at 15:19
@aman did you just copy code from an existing answer? You should vote close as a duplicate, not copy someone else's work (even if you provided a link back).
– freedomn-m
Nov 9 at 15:20
1
With the code I posted above I believe you can do something like: PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Company.com", "DC=SomeDC,DC=COM", username, Password); where you pass your username and password. Rest code remains same
– aman
Nov 9 at 15:55
Hi Aman, thank you so much, but I need to validate the users password. I dont want to allow them to do something if password does not match. Can you advice how I can do that?
– edcoder
Nov 9 at 14:36
Hi Aman, thank you so much, but I need to validate the users password. I dont want to allow them to do something if password does not match. Can you advice how I can do that?
– edcoder
Nov 9 at 14:36
@edcoder In your post you had mentioned that you were able to authenticate user and need to check if user belongs to the group. Thats why I shared above code. You might need to update your post and correct this. You would also need to provide details against what do you want your user to authenticate. Do you have some database or if its a windows login etc.
– aman
Nov 9 at 14:43
@edcoder In your post you had mentioned that you were able to authenticate user and need to check if user belongs to the group. Thats why I shared above code. You might need to update your post and correct this. You would also need to provide details against what do you want your user to authenticate. Do you have some database or if its a windows login etc.
– aman
Nov 9 at 14:43
@edcoder your post clearly states that you want to check if the user is in an AD group. There is no mention of checking passwords. Please ask a new question.
– freedomn-m
Nov 9 at 15:19
@edcoder your post clearly states that you want to check if the user is in an AD group. There is no mention of checking passwords. Please ask a new question.
– freedomn-m
Nov 9 at 15:19
@aman did you just copy code from an existing answer? You should vote close as a duplicate, not copy someone else's work (even if you provided a link back).
– freedomn-m
Nov 9 at 15:20
@aman did you just copy code from an existing answer? You should vote close as a duplicate, not copy someone else's work (even if you provided a link back).
– freedomn-m
Nov 9 at 15:20
1
1
With the code I posted above I believe you can do something like: PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Company.com", "DC=SomeDC,DC=COM", username, Password); where you pass your username and password. Rest code remains same
– aman
Nov 9 at 15:55
With the code I posted above I believe you can do something like: PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Company.com", "DC=SomeDC,DC=COM", username, Password); where you pass your username and password. Rest code remains same
– aman
Nov 9 at 15:55
|
show 3 more comments
up vote
0
down vote
Here is how I solved this :
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
adsSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + userid + "))";
try
SearchResult adsSearchResult = adsSearcher.FindOne();
string propertyName = "memberOf";
ResultPropertyValueCollection rpvcResult = adsSearchResult.Properties[propertyName];
foreach (Object PropertyValue in rpvcResult)
if (PropertyValue.ToString() == "Group Name")
user.Verified = true;
user.FullName = GetFullName(userid);
adsEntry.Close();
else
user.Verified = false;
user.error = "You do not belong to the Group so you cannot do this function";
catch (Exception ex)
user.error = "Please check your username and password credentials";
adsEntry.Close();
add a comment |
up vote
0
down vote
Here is how I solved this :
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
adsSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + userid + "))";
try
SearchResult adsSearchResult = adsSearcher.FindOne();
string propertyName = "memberOf";
ResultPropertyValueCollection rpvcResult = adsSearchResult.Properties[propertyName];
foreach (Object PropertyValue in rpvcResult)
if (PropertyValue.ToString() == "Group Name")
user.Verified = true;
user.FullName = GetFullName(userid);
adsEntry.Close();
else
user.Verified = false;
user.error = "You do not belong to the Group so you cannot do this function";
catch (Exception ex)
user.error = "Please check your username and password credentials";
adsEntry.Close();
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is how I solved this :
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
adsSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + userid + "))";
try
SearchResult adsSearchResult = adsSearcher.FindOne();
string propertyName = "memberOf";
ResultPropertyValueCollection rpvcResult = adsSearchResult.Properties[propertyName];
foreach (Object PropertyValue in rpvcResult)
if (PropertyValue.ToString() == "Group Name")
user.Verified = true;
user.FullName = GetFullName(userid);
adsEntry.Close();
else
user.Verified = false;
user.error = "You do not belong to the Group so you cannot do this function";
catch (Exception ex)
user.error = "Please check your username and password credentials";
adsEntry.Close();
Here is how I solved this :
DirectoryEntry adsEntry = new DirectoryEntry("domain", userid, password);
DirectorySearcher adsSearcher = new DirectorySearcher(adsEntry);
adsSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + userid + "))";
try
SearchResult adsSearchResult = adsSearcher.FindOne();
string propertyName = "memberOf";
ResultPropertyValueCollection rpvcResult = adsSearchResult.Properties[propertyName];
foreach (Object PropertyValue in rpvcResult)
if (PropertyValue.ToString() == "Group Name")
user.Verified = true;
user.FullName = GetFullName(userid);
adsEntry.Close();
else
user.Verified = false;
user.error = "You do not belong to the Group so you cannot do this function";
catch (Exception ex)
user.error = "Please check your username and password credentials";
adsEntry.Close();
answered 7 hours ago
edcoder
16411
16411
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53227138%2ffind-if-user-belongs-to-group%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Possible duplicate of How to check if a user belongs to an AD group?
– freedomn-m
Nov 9 at 15:20
I talked a bit about that in an article I wrote here: gabescode.com/active-directory/2018/09/13/…
– Gabriel Luci
Nov 9 at 15:27
Thanks Gabriel, Sorry I just updated my question. User fills a form to enter credentials. so I cant use the windows authenication method. I will need to use a different way if you know what I mean
– edcoder
Nov 9 at 15:40
That's fine. My article discussed doing the same with
DirectoryEntry. But you will have to search for their user account first. There are lots of examples online of finding a user by username withDirectorySearcher.– Gabriel Luci
Nov 9 at 16:00
Thank You Gabriel..will update how I go about it. Your articles are really good.
– edcoder
Nov 10 at 8:56