【发布时间】:2013-08-26 19:54:32
【问题描述】:
我有一个使用 Windows 身份验证的 .NET MVC 4 应用程序。有些用户是管理员,需要能够代表其他用户输入数据。
我有一个文本框,管理员可以在其中输入另一个用户的姓名。如何检查以确认输入的文本是现有的 Windows 用户名?
【问题讨论】:
标签: c# .net asp.net-mvc-4 windows-authentication
我有一个使用 Windows 身份验证的 .NET MVC 4 应用程序。有些用户是管理员,需要能够代表其他用户输入数据。
我有一个文本框,管理员可以在其中输入另一个用户的姓名。如何检查以确认输入的文本是现有的 Windows 用户名?
【问题讨论】:
标签: c# .net asp.net-mvc-4 windows-authentication
您可以使用FindByIdentity 方法:
string username = "Some username you retrieved from the TextBox";
using (var ctx = new PrincipalContext(ContextType.Domain, "YOUR_DOMAIN"))
using (var user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username))
{
bool userExists = user != null;
// here you know whether the user exists or not
}
【讨论】:
您可以为此查询您组织的 Active Directory。
DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName");
DirectorySearcher Dsearch = new DirectorySearcher(entry);
String Name="Richmond";
dSearch.Filter = "(&(objectClass=user)(l=" + Name + "))";
请看这篇文章: http://www.codeproject.com/Articles/6778/How-to-get-User-Data-from-the-Active-Directory
【讨论】: