EDN Admin
Well-known member
I have a C#.net windows form application 2008 application. I would like to know how to make the following code work so I can see if users belong to three different groups in the active directory. i was told by my company that I need to make this code work.
The code is called from:<br/>
CurUser = new ActiveDirectoryUser();<br/>
bool isAuthenticated = CurUser.IsAuthenticated(domain, user, password);
using System;<br/>
using System.Collections.Generic;<br/>
using System.Text;<br/>
using System.DirectoryServices;
namespace Common.Area<br/>
{<br/>
public class ActiveDirectoryUser<br/>
{<br/>
private string _path;<br/>
private string _filterAttribute;
//public ActiveDirectoryUser(string path)<br/>
public ActiveDirectoryUser()
{<br/>
//_path = path;<br/>
_path = " LDAP://Omaha.Reed/DC=OMAHA,DC=REED ";<br/>
}
public bool IsAuthenticated(string domainName, string userName, string password)<br/>
{<br/>
string domainAndUsername = domainName + @"" + userName;<br/>
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, password);<br/>
try<br/>
{<br/>
// Bind to the native AdsObject to force authentication.<br/>
Object obj = entry.NativeObject;<br/>
DirectorySearcher search = new DirectorySearcher(entry);<br/>
search.Filter = "(SAMAccountName=" + userName + ")";<br/>
search.PropertiesToLoad.Add("cn");<br/>
SearchResult result = search.FindOne();<br/>
<br/>
if (null == result)<br/>
{<br/>
return false;<br/>
}<br/>
// Update the new path to the user in the directory<br/>
_path = result.Path;<br/>
_filterAttribute = (String)result.Properties["cn"][0];<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
throw new Exception(ex.Message);<br/>
}<br/>
return true;<br/>
}
}<br/>
}
View the full article
The code is called from:<br/>
CurUser = new ActiveDirectoryUser();<br/>
bool isAuthenticated = CurUser.IsAuthenticated(domain, user, password);
using System;<br/>
using System.Collections.Generic;<br/>
using System.Text;<br/>
using System.DirectoryServices;
namespace Common.Area<br/>
{<br/>
public class ActiveDirectoryUser<br/>
{<br/>
private string _path;<br/>
private string _filterAttribute;
//public ActiveDirectoryUser(string path)<br/>
public ActiveDirectoryUser()
{<br/>
//_path = path;<br/>
_path = " LDAP://Omaha.Reed/DC=OMAHA,DC=REED ";<br/>
}
public bool IsAuthenticated(string domainName, string userName, string password)<br/>
{<br/>
string domainAndUsername = domainName + @"" + userName;<br/>
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, password);<br/>
try<br/>
{<br/>
// Bind to the native AdsObject to force authentication.<br/>
Object obj = entry.NativeObject;<br/>
DirectorySearcher search = new DirectorySearcher(entry);<br/>
search.Filter = "(SAMAccountName=" + userName + ")";<br/>
search.PropertiesToLoad.Add("cn");<br/>
SearchResult result = search.FindOne();<br/>
<br/>
if (null == result)<br/>
{<br/>
return false;<br/>
}<br/>
// Update the new path to the user in the directory<br/>
_path = result.Path;<br/>
_filterAttribute = (String)result.Properties["cn"][0];<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
throw new Exception(ex.Message);<br/>
}<br/>
return true;<br/>
}
}<br/>
}
View the full article