我有一个 MVC4 内网页面,想获取 homeDirectory
来自 Active Directory 的属性。想知道从 AD 获取属性的最快方法。
最佳答案
由于您使用的是 .NET 3.5 及更高版本,您应该查看 System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关信息:
基本上,您可以定义域上下文并轻松找到 AD 中的用户和/或组:
// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// do something here....
string homeDrive = user.HomeDrive;
string homeDirectory = user.HomeDirectory;
}
}
如果您在 ASP.NET MVC 应用程序中使用 Windows 身份验证,您还可以像这样获取当前登录的用户:
UserPrincipal currentUser = UserPrincipal.Current;
但通常情况下,在网络应用程序中,这类似于
NETWORK SERVICE
或 IUSER_machineName
用户(而不是您在浏览器上的用户)...新的 S.DS.AM 使在 AD 中与用户和组一起玩变得非常容易!
关于asp.net - 如何在MVC 4中查询当前登录用户的AD信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13793126/