C#检查,如果一组用户会员吗? [英] c# check if the user member of a group?
本文介绍了C#检查,如果一组用户会员吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有,我用它来检查,如果用户是AD的成员code,工作完美,
现在我想补充的可能性,以检查用户是否也有组的成员!
我需要做什么修改,以实现这一目标,我做了一些工作,但它失败!
所以这里是我的code:
//验证用户对目录
私人布尔验证(用户名字符串,字符串密码,串域)
{
如果(用户名==||密码==)
{
返回false;
}
布尔正宗= FALSE;
尝试
{
的DirectoryEntry条目=新的DirectoryEntry(LDAP://+域名,用户名,密码);
反对nativeObject = entry.NativeObject;
正宗= TRUE;
}
赶上(DirectoryServicesCOMException){}
返回正宗的;
}
我想让它是这样的:
私人布尔验证(用户名字符串,字符串密码,串域,弦乐群)
解决方案
我解决这个code
公共BOOL AuthenticateGroup(用户名字符串,字符串密码,串域,弦乐群)
{
如果(用户名==||密码==)
{
返回false;
}
尝试
{
的DirectoryEntry条目=新的DirectoryEntry(LDAP://+域名,用户名,密码);
DirectorySearcher从mySearcher =新DirectorySearcher从(输入);
mySearcher.Filter =(及(objectClass的=用户)(|(CN =+的userName +)(sAMAccountName赋=+的userName +)));
信息搜索结果的结果= mySearcher.FindOne();
的foreach(在result.Properties字符串GroupPath [成员])
{
如果(GroupPath.Contains(组))
{
返回true;
}
}
}
赶上(DirectoryServicesCOMException)
{
}
返回false;
}
它正常工作对我来说,它可以是一台机器的域控制器不是一部分使用/ Active Directory的
感谢大家的帮助,
I have a code that I use to check if the user is member of the AD, worked perfectly,
now I want to add the possibility to check if the user also a member of a group!
what do I need to modify to achieve that, I did some work, but it fails!
so here is my code:
//Authenticate a User Against the Directory
private bool Authenticate(string userName,string password, string domain)
{
if (userName == "" || password == "")
{
return false;
}
bool authentic = false;
try
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,userName, password);
object nativeObject = entry.NativeObject;
authentic = true;
}
catch (DirectoryServicesCOMException) { }
return authentic;
}
I want to make it like this:
private bool Authenticate(string userName,string password, string domain, string group)
解决方案
I solve it with this code
public bool AuthenticateGroup(string userName, string password, string domain, string group)
{
if (userName == "" || password == "")
{
return false;
}
try
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";
SearchResult result = mySearcher.FindOne();
foreach (string GroupPath in result.Properties["memberOf"])
{
if (GroupPath.Contains(group))
{
return true;
}
}
}
catch (DirectoryServicesCOMException)
{
}
return false;
}
it works fine for me, and it can be use with a machine not part of the Domain Controller / Active Directory
Thank you all for the help
这篇关于C#检查,如果一组用户会员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文