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();










share|improve this question























  • 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 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














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();










share|improve this question























  • 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 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












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();










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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 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















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












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?






share|improve this answer




















  • 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

















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();






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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?






    share|improve this answer




















    • 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














    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?






    share|improve this answer




















    • 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












    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?






    share|improve this answer












    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?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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
















    • 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












    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();






    share|improve this answer
























      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();






      share|improve this answer






















        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();






        share|improve this answer












        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();







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 7 hours ago









        edcoder

        16411




        16411



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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














































































            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus