62,271
社区成员
发帖
与我相关
我的任务
分享
private bool IsAuthenticated(String username, String pwd)
{
;
String server = ConfigurationManager.AppSettings["ADServer"];//192.168.18.201
String path = ConfigurationManager.AppSettings["ADPath"];//smallbusiness.com
String domainAndUsername = username;
DirectoryEntry entry;
entry = new DirectoryEntry(@"LDAP://" + server , domainAndUsername, pwd, AuthenticationTypes.Secure);
try
{
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (result == null)
{
return false;
}
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
return false;
}
}
#region 变量 variable
private string sDomain = "192.168.18.201";
private string sDefaultOU = "OU=Union,OU=Domain Controllers,DC=business,DC=com";
private string sDefaultRootOU = "DC=business,DC=com";
private string sServiceUser = @"badyue";
private string sServicePassword = "P@ssw0rd";
#endregion
#region 验证方法(Validate Methods)
/// <summary>
/// 对一个已经存在的用户,验证用户名和密码
/// </summary>
/// <param name="sUserName">用户名</param>
/// <param name="sPassword"> 密码</param>
/// <returns>是否有效</returns>
public bool ValidateCredentials(string sUserName, string sPassword,string OU)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
return oPrincipalContext.ValidateCredentials(sUserName, sPassword);
}
/// <summary>
/// Creates a new group in Active Directory
/// </summary>
/// <param name="sOU">The OU location you want to save your new Group</param>
/// <param name="sGroupName">The name of the new group</param>
/// <param name="sDescription">The description of the new group</param>
/// <param name="oGroupScope">The scope of the new group</param>
/// <param name="bSecurityGroup">True is you want this group to be a security group, false if you want this as a distribution group</param>
/// <returns>Retruns the GroupPrincipal object</returns>
public GroupPrincipal CreateNewGroup(string sOU, string sGroupName, string sDescription, GroupScope oGroupScope, bool bSecurityGroup)
{
PrincipalContext oPrincipalContext = GetPrincipalContext(sOU);
GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext, sGroupName);
oGroupPrincipal.Description = sDescription;
oGroupPrincipal.GroupScope = oGroupScope;
oGroupPrincipal.IsSecurityGroup = bSecurityGroup;
oGroupPrincipal.Save();
return oGroupPrincipal;
}
/// <summary>
/// Gets the base principal context
/// </summary>
/// <returns>Retruns the PrincipalContext object</returns>
public PrincipalContext GetPrincipalContext()
{
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
return oPrincipalContext;
}
/// <summary>
/// Gets the principal context on specified OU
/// </summary>
/// <param name="sOU">The OU you want your Principal Context to run on</param>
/// <returns>Retruns the PrincipalContext object</returns>
public PrincipalContext GetPrincipalContext(string sOU)
{
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain,sOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
return oPrincipalContext;
}

private string sServiceUser = @"badyue";
private string sServicePassword = "P@ssw0rd"; 这个得把用户名和密码换成域的administrator