public static bool IsAuthenticUserLDAP(this UserModel model)
{
string propertyName = "Name";
bool isAuthenticated = false;
string username = model.UserName;
string domain = model.DomainName;
string password = model.Password;
DirectoryContext context = new DirectoryContext(DirectoryContextType.Forest, domain, username, password);
try
{
Forest forest = Forest.GetForest(context);
GlobalCatalog gc = null;
gc = forest.FindGlobalCatalog();
if (gc != null)
{
DirectorySearcher searcher = gc.GetDirectorySearcher();
searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + username + ")(memberof=CN=PRM_WEB_,OU=Groups,OU=CLS,OU=NA-Organizations,DC=na,DC=wkglobal,DC=com))";
foreach (SearchResult result in searcher.FindAll())
{
if (result != null)
{
DirectoryEntry personEntry = result.GetDirectoryEntry();
if (personEntry.Name != null)
{
model.Name = result.Properties[propertyName][0].ToString();
model.UserType = Constants.CONST_ADIMIN_USERTYPE;
isAuthenticated = true;
}
else
{
isAuthenticated = false;
}
}
}
if (isAuthenticated == false)
{
searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + username + ")(memberof=CN=PRM_WEB_,OU=Groups,OU=CLS,OU=NA-Organizations,DC=na,DC=wkglobal,DC=com))";
foreach (SearchResult result in searcher.FindAll())
{
if (result != null)
{
DirectoryEntry personEntry = result.GetDirectoryEntry();
if (personEntry.Name != null)
{
model.Name = result.Properties[propertyName][0].ToString();
model.UserType = Constants.CONST_NORMAL_USERTYPE;
isAuthenticated = true;
}
else
{
isAuthenticated = false;
}
}
}
}
}
}
catch (AuthenticationException ex)
{
ErrorLogUtil.LogMessage("ExtentionMethod", "IsAuthenticUser", ex, Enums.LogType.Error);
}
return isAuthenticated;
}