我有一个使用Windows身份验证的.NET MVC 4应用程序。某些用户是管理员,需要能够代表其他用户输入数据。使用Windows身份验证时验证另一个用户
我有一个文本框,管理员输入另一个用户的名字。如何检查以确认输入的文本是现有的Windows用户名?
我有一个使用Windows身份验证的.NET MVC 4应用程序。某些用户是管理员,需要能够代表其他用户输入数据。使用Windows身份验证时验证另一个用户
我有一个文本框,管理员输入另一个用户的名字。如何检查以确认输入的文本是现有的Windows用户名?
你可以使用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
}
您可以查询您的组织的活动目录这一点。
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
其实有一个比DirectoryEntry更新的API:http://msdn.microsoft.com/en-us/library/bb344891.aspx –
任何理由downvote?请在回答问题时留下评论,说明您为什么认为此答案错误。 –
不知道这个问题和这些答案的所有仇恨背后是什么,但这对我有效。谢谢。 –