使用 C# 从 ASP.Net MVC 访问 Active Directory [英] Accessing Active Directory from ASP.Net MVC using C#

查看:25
本文介绍了使用 C# 从 ASP.Net MVC 访问 Active Directory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问 Active Directory 以获取有关客户所属组的信息.我的项目是一个使用 C# 的 ASP.Net MVC 应用程序.我以前从未针对 Active Directory 进行过编程,因此需要一些关于最佳入门方法、用于访问信息的安全模型的建议,并且可能会为我提供一些不错的教程.

I need to access Active Directory to get information about groups that customers belong to. The project I have is an ASP.Net MVC application using C#. I've never programmed against Active Directory before, and need some advice on what the best way to get started is, what security model to use to access the information, and maybe point me to some good tutorials.

推荐答案

由于您使用的是 MVC,您可以访问新的 System.DirectoryServices.AccountManagement .NET 3.5 中的命名空间.这些类应该优先于 DirectoryServices 本身中的旧类,因为它们使用起来要简单得多.有几个问题在 3.5 中尚未解决(例如,查询组时限制 1500 个成员),但我确信这些问题已在 .NET 4.0 中得到修复.对于大多数任务,新类都能很好地工作.

Since you're using MVC, you have access to the new System.DirectoryServices.AccountManagement namespace in .NET 3.5. These classes should be preferred over the older classes in DirectoryServices itself as they are much simpler to use. There are a couple of gotchas that haven't been solved in 3.5 (1500 member limit when querying groups, for instance), but I'm assured that these have been fixed in .NET 4.0. For most tasks, the new classes work very well.

 using (var context = new PrincipalContext( ContextType.Domain )) 
 {
      using (var user = UserPrincipal.FindByIdentity( context, "username" ))
      {
          var groups = user.GetAuthorizationGroups();
          ...
      }
 }

这篇关于使用 C# 从 ASP.Net MVC 访问 Active Directory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆