Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:46:27+00:00 2026-06-11T15:46:27+00:00

I have a web application using Windows Authentication in C# and currently I assign

  • 0

I have a web application using Windows Authentication in C# and currently I assign users to roles individually.

e.g. At each page of the application, I check

if(Roles.IsUserInRole(AU\UserName, "PageAccessRole"))

As I need to roll out the application to the whole team this week (and eventually the whole company), I need to user AD groups as there are over 3000 ppl so I am not about to do it manually!

As a newbie to ASP.NET (and programming in general) and I really don’t know much about setting up AD groups (e.g. how do I get access to the AD groups from my application etc?)

I would be soooo grateful if anyone can point me in the right direction…I’ve been reading up all about LDAP and System.DirectoryServices.AccountManagement etc but I am just getting all the more confused.

So far, I have this in my web.config

  <authentication mode="Windows">
  </authentication>
  <authorization> 
              <allow roles="AU\Active Directory Group Name"/>
    <deny users="?"/>
  </authorization>

  <roleManager enabled="true" >
    <providers>
    <clear/>
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>

And I’ve enabled Windows Authentication and disabled Anonymous in the IIS Server.

Please please help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:46:28+00:00Added an answer on June 11, 2026 at 3:46 pm

    Solutions:-

    This is how you can Fetch Groups from an OU in AD

    DataTable dt = new DataTable();
    dt.Columns.Add("groups");
    DirectoryEntry rootDSE = null;
    

    Suppose I want to fetch records from my Department OU. Now the Path would be like that

    Department–>>Users

    and dc here is Domain Controller name, In my case it was Corp.Local
    In this way you can fetch groups from your AD

    if (department != "")
    {
       rootDSE = new DirectoryEntry(
         "LDAP://OU=" + department + ",OU=Users,dc=corp,dc=local", username, password);
    }
    else
    {
       rootDSE = new DirectoryEntry(
          "LDAP://OU=Users,OU=" + ou + ",dc=corp,dc=local", username, password);
    }
    DirectorySearcher ouSearch = new DirectorySearcher(rootDSE);
    ouSearch.PageSize = 1001;
    ouSearch.Filter = "(objectClass=group)";
    ouSearch.SearchScope = SearchScope.Subtree;
    ouSearch.PropertiesToLoad.Add("name");
    SearchResultCollection allOUS = ouSearch.FindAll();
    foreach (SearchResult oneResult in allOUS)
    {
        dt.Rows.Add(oneResult.Properties["name"][0].ToString());
    }
    rootDSE.Dispose();
    return dt;
    

    Now how to add Users to the groups.

    It is an example for a single user, you can do this in similar way by Looping the Users.

     PrincipalContext pr = new PrincipalContext(ContextType.Domain,
         "corp.local", "dc=corp,dc=local", username, password);
    GroupPrincipal group = GroupPrincipal.FindByIdentity(pr, groupName);//Looking for the Group in AD Server
    
    if (group == null)
      {
         //Throw Exception
      }
    
    UserPrincipal user = UserPrincipal.FindByIdentity(pr, userName);//Looking  for the User in AD Server
    
    if (user.IsMemberOf(group))//If Group is already added to the user
       {
           //I have Put it into If else condition because in case you want to Remove Groups from that User you can write your Logic here.
    
         //Do Nothing, Because the group is already added to the user
       }
     else// Group not found in the Current user,Add it
       {
          if (user != null & group != null)
           {
             group.Members.Add(user);
             group.Save();
             done = user.IsMemberOf(group);//You can confirm it from here
            }
       }
         pr.Dispose();
         return done;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today we have a windows application that, using an OCX, creates a web page
I have a web application that runs off the domain: test.com using Windows Authentication.
We have implemented SSO in a .NET web application using Windows Identity Foundation (WIF).
I have this web application using Spring Web Flow framework. In my main page
I have a web application set up on our intranet that uses windows authentication
I have an intranet application using windows authentication that works great. Now I have
I have forms authentication already built in my web application. Can I have Windows
I have an ASP.Net application that is using Windows Authentication and I have specified
In our asp.net intranet application we are using windows authentication to authenticate the users.
I have a web application that allows anonymous access and windows authentication. Basically, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.