使用Windows身份验证时正在对另一个用户进行身份验证

本文关键字:身份验证 另一个 用户 Windows 使用 | 更新日期: 2023-09-27 18:29:08

我有一个使用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
}

您可以为此查询组织的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