C# 做AD 验证的问题 急..

michael008 2010-10-19 08:21:22
有问题了兄弟们,我在做AD验证.现在有两个DC 一个是192.168.18.201, 域为SMALLBUSINESS.COM 有两个用户,一个是192.168.18.202,有一个用户,域是Child.SMALLBUSINESS, 是子域,我想做个程序 把两个域的用户都可以验证并添加修改..
可是我用下面的代码

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;
}
}


不知道怎么用域smallbusiness.com,我加上的话new DirectoryEntry(@"LDAP://" + server+",DC=SMALLBUSINESS,DC=COM" , domainAndUsername, pwd, AuthenticationTypes.Secure); 说指定的域不存在,或无法联系。

我再用下面的


#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;
}


当我用 ValidateCredentials'; 的方法的时候 能验证 192.168.18.201 的用户 验证不了 192.168.18.202 上的用户,I can get the right result,but when i use the method'当我用 CreateNewGroup' ,这个方法建组的时候 却说用户名和密码错但是 'administrator' 和他的密码'P@ssw0rd' 确实是对的 我应该怎么办....




谢谢兄弟们了
...全文
293 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mayanly 2010-10-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 michael008 的回复:]
事实上badyue 已经是 Administrators组了 我换过 用administrator 也一样的

关键是如何验证两台机器 上的 用户啊 只用一个程序,
[/Quote]这个是什么意思没太看懂
xiangaylian 2010-10-21
  • 打赏
  • 举报
回复
有人!!
不过俺不懂!!
mark620 2010-10-19
  • 打赏
  • 举报
回复
xuan.ye 2010-10-19
  • 打赏
  • 举报
回复
域验证就是麻烦
michael008 2010-10-19
  • 打赏
  • 举报
回复
来人啊 急死我了
michael008 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 int64 的回复:]
AD,DC都是啥
[/Quote] AD 是 » Active Directory ,DC 是Domain Collacatino
michael008 2010-10-19
  • 打赏
  • 举报
回复
事实上badyue 已经是 Administrators组了 我换过 用administrator 也一样的

关键是如何验证两台机器 上的 用户啊 只用一个程序,
mayanly 2010-10-19
  • 打赏
  • 举报
回复
当我用 CreateNewGroup' ,这个方法建组的时候 却说用户名和密码错但是 'administrator' 和他的密码'P@ssw0rd' 确实是对的 我应该怎么办....这个应该只有管理员有权限吧?所以我认为
private string sServiceUser = @"badyue";   
private string sServicePassword = "P@ssw0rd";
这个得把用户名和密码换成域的administrator
int64 2010-10-19
  • 打赏
  • 举报
回复
AD,DC都是啥
bychgh 2010-10-19
  • 打赏
  • 举报
回复
不懂,帮顶~~~~~~~~~~

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧