You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.7 KiB
111 lines
3.7 KiB
using System;
|
|
using System.Text;
|
|
using System.Collections;
|
|
using System.DirectoryServices;
|
|
using System.DirectoryServices.AccountManagement;
|
|
|
|
namespace OT.Web.Ap_Code
|
|
{
|
|
public class LdapAuthentication
|
|
{
|
|
private String _path;
|
|
private String _filterAttribute;
|
|
|
|
public LdapAuthentication(String path)
|
|
{
|
|
_path = path;
|
|
}
|
|
|
|
public bool IsAuthenticated(String domain, String username, String pwd)
|
|
{
|
|
String domainAndUsername = domain + @"\" + username;
|
|
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
|
|
|
|
try
|
|
{ //Bind to the native AdsObject to force authentication.
|
|
|
|
//PrincipalContext ctx = new PrincipalContext(
|
|
// ContextType.Domain,
|
|
// "origtek.com.cn",
|
|
// "CN=Users,DC=origtek,DC=com.cn",
|
|
// "administrator",
|
|
// "");
|
|
|
|
//UserPrincipal usr = new UserPrincipal(ctx);
|
|
|
|
//usr.Name = "Jim Daly";
|
|
//usr.Description = "This is the user account for Jim Daly";
|
|
//usr.EmailAddress = "jimdaly@fabrikam.com";
|
|
//usr.SetPassword("securelyStoredPassword");
|
|
//usr.Save();
|
|
|
|
//usr.Dispose();
|
|
//ctx.Dispose();
|
|
//PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain);
|
|
//UserPrincipal usr = new UserPrincipal(ctx, domainAndUsername, pwd, false);
|
|
|
|
|
|
Object obj = entry.NativeObject;
|
|
DirectorySearcher search = new DirectorySearcher(entry);
|
|
|
|
search.Filter = "(SAMAccountName=" + username + ")";
|
|
search.PropertiesToLoad.Add("cn");
|
|
SearchResult result = search.FindOne();
|
|
|
|
if (null == result)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
//Update the new path to the user in the directory.
|
|
_path = result.Path;
|
|
_filterAttribute = (String)result.Properties["cn"][0];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Error authenticating user. " + ex.Message);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public String GetGroups()
|
|
{
|
|
DirectorySearcher search = new DirectorySearcher(_path);
|
|
search.Filter = "(cn=" + _filterAttribute + ")";
|
|
search.PropertiesToLoad.Add("memberOf");
|
|
StringBuilder groupNames = new StringBuilder();
|
|
|
|
try
|
|
{
|
|
SearchResult result = search.FindOne();
|
|
|
|
int propertyCount = result.Properties["memberOf"].Count;
|
|
|
|
String dn;
|
|
int equalsIndex, commaIndex;
|
|
|
|
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
|
|
{
|
|
dn = (String)result.Properties["memberOf"][propertyCounter];
|
|
|
|
equalsIndex = dn.IndexOf("=", 1);
|
|
commaIndex = dn.IndexOf(",", 1);
|
|
if (-1 == equalsIndex)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
|
|
groupNames.Append("|");
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Error obtaining group names. " + ex.Message);
|
|
}
|
|
return groupNames.ToString();
|
|
}
|
|
}
|
|
}
|