問題描述
我需要訪問 Active Directory 以獲取有關(guān)客戶所屬組的信息.我的項(xiàng)目是一個(gè)使用 C# 的 ASP.Net MVC 應(yīng)用程序.我以前從未針對(duì) Active Directory 進(jìn)行過編程,因此需要一些關(guān)于最佳入門方法、用于訪問信息的安全模型的建議,并且可能會(huì)為我提供一些不錯(cuò)的教程.
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 中的命名空間.這些類應(yīng)該優(yōu)先于 DirectoryServices 本身中的舊類,因?yàn)樗鼈兪褂闷饋硪唵蔚枚?有幾個(gè)問題在 3.5 中尚未解決(例如,查詢組時(shí)限制 1500 個(gè)成員),但我確信這些問題已在 .NET 4.0 中得到修復(fù).對(duì)于大多數(shù)任務(wù),新類都能很好地工作.
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();
...
}
}
這篇關(guān)于使用 C# 從 ASP.Net MVC 訪問 Active Directory的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!