使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Finding Active Directory users from 2 OU(从 2 个 OU 中查找 Active Directory 用户)
How to set a binary attribute when using a AccountManagement Extension Class?(使用 AccountManagement 扩展类时如何设置二进制属性?)
Getting last Logon Time on Computers in Active Directory(在 Active Directory 中的计算机上获取上次登录时间)
customer-configurable asp.net web site security for fine-grained control of page and button access(客户可配置的 asp.net 网站安全性,用于对页面和按钮访问进行细粒度控制)
Active Directory - Roles of a user(Active Directory - 用户的角色)
How to connect to Active Directory via LDAPS in C#?(如何在 C# 中通过 LDAPS 连接到 Active Directory?)