【问题标题】:How to set up access for users in Active Directory group如何为 Active Directory 组中的用户设置访问权限
【发布时间】:2012-09-17 05:56:36
【问题描述】:

我有一个在 C# 中使用 Windows 身份验证 的 Web 应用程序,目前我将用户单独分配给角色。

例如在应用程序的每个页面上,我都会检查

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

由于我需要在本周将应用程序推广到整个团队(最终是整个公司),我需要使用 AD 组,因为有超过 3000 人,所以我不打算手动执行!

作为 ASP.NET(以及一般编程)的新手,我真的不太了解设置 AD 组(例如,如何从我的应用程序访问 AD 组等?)

如果有人能指出我正确的方向,我将不胜感激...我一直在阅读有关 LDAPSystem.DirectoryServices.AccountManagement 等的所有内容但我越来越糊涂了。

到目前为止,我的 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>

我在 IIS 服务器中启用了 Windows 身份验证并禁用了匿名。

请帮忙!!

【问题讨论】:

  • 我怀疑这个线程 (stackoverflow.com/questions/2188954/…) 会很有用,但我不知道如何将代码合并到我的应用程序中...
  • 您要添加新组并将人员添加到组吗?
  • 不,我想使用公司现有的 Active Directory 组!
  • 我不想手动将数千人添加到一个组中等。
  • 我可以在这里向您展示的是使用 Foreach 循环将一堆用户添加到 Active Directory 组。如果您想要相同的内容,请告诉我,我可以在这里向您展示一个示例。

标签: c# asp.net authentication active-directory


【解决方案1】:

解决方案:-

这是在 AD 中从 OU 中获取组的方法

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

假设我想从我的部门 OU 获取记录。现在的路径是这样的

部门-->>用户

dc 这里是域控制器名称,在我的例子中是 Corp.Local
通过这种方式,您可以从 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;

现在如何将用户添加到组中。

这是单个用户的示例,您可以通过循环用户以类似的方式执行此操作。

 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;

【讨论】:

  • 另一个问题,如果你能帮助我!我只使用了您代码的第一部分(即检查是否登录了特定 AD 组的用户部分)。现在,如果用户是该 AD 组的一部分,我想允许他们对应用程序拥有某些安全权限。我创建了一个虚拟用户,分配了我希望 AD 组的每个人都拥有的角色。如何允许用户“继承”虚拟用户的安全权限?
  • 基本上,我只是希望 AD 组的那些部分具有一定的安全权限(而不是为每个用户添加单独的安全权限)。
  • 我刚刚意识到我应该一直在 web.config 文件中使用 System.Web.Security.WindowsTokenRoleProvider 和 System.Web.Security.ActiveDirectoryMembershipProvider(而不是上面的代码)!
  • 感谢您的所有帮助...如果您也可以帮助解决我来自stackoverflow.com/questions/12559457/… 的新问题,我们将不胜感激!
猜你喜欢
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多