using System.DirectoryServices.AccountManagement;
...
try
{
var principalContext = new PrincipalContext(ContextType.Domain, domain);
using (
var user = UserPrincipal.FindByIdentity(
principalContext, IdentityType.SamAccountName, account))
{
if (user == null)
{
ModelState.AddModelError("UserName","UserNotExistInAD");
}
}
}
catch (PrincipalServerDownException)
{
ModelState.AddModelError("UserName", "DomainNotExistOrNotAvailable");
}
catch (PrincipalException)
{
ModelState.AddModelError("UserName", "UserNotExistInAD");
}
【User】How to check if a user name belongs to a domain
最新推荐文章于 2019-07-30 13:49:42 发布