使用 C# 从 ASP.Net MVC 访问 Active Directory

Accessing Active Directory from ASP.Net MVC using C#(使用 C# 从 ASP.Net MVC 访问 Active Directory)
本文介绍了使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
How to store delegates in a List(如何将代表存储在列表中)
How delegates work (in the background)?(代表如何工作(在后台)?)
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)