Active Directory(活动目录)是Windows Server 2003域环境中提供目录服务的组件。目录服务在微软平台上从Windows Server 2000开始引入,所以我们可以理解为活动目录是目录服务在微软平台的一种实现方式。当然目录服务在非微软平台上都有相应的实现。关于AD的更详细说明请上百度度娘吧,哈哈~
要在C#.net平台下实现AD相关操作,需要添加引用using System.DirectoryServices;
1.AD验证登入,登入窗体如下:
private void Login_Click(object sender, EventArgs e) //登入按钮
{
try
{
string FilterStr;//要查找的用户名
DirectoryEntry entry = this.MyGetDirectoryEntry();
DirectorySearcher Seacher = new DirectorySearcher(entry);
FilterStr = "(&(objectClass=user) (cn=" + m_username + "))";
Seacher.Filter = FilterStr;
SearchResult Result = Seacher.FindOne();
if (Result == null)
{
MessageBox.Show("用戶名或密碼有錯!");
}
else
{
MessageBox.Show("登入成功!");
entry.AuthenticationType = AuthenticationTypes.Secure;
//this.Hide();
//Form2 frm = new Form2();
//frm.Show();
}
}
catch (Exception ex)
{
string Str = ex.Message;
MessageBox.Show("用戶名或密碼有錯!");
}
}
private void Check_Click(object sender, EventArgs e) //测试按钮
{
m_username = UserName.Text;
if (UserExists(m_username))
{
MessageBox.Show("連接成功!");
// CreateNewUser("", "ezhrt1");
}
else
{
MessageBox.Show("連接失敗!");
}
}
public bool UserExists(string UserName) // 判断用户是否存在
{
try
{
DirectoryEntry entry = this.MyGetDirectoryEntry();
DirectorySearcher Search = new DirectorySearcher();
Search.SearchRoot = entry;
Search.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
SearchResultCollection results = Search.FindAll();
if (results.Count == 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
string Str = ex.Message;
return false;
}
}
2.添加用户
private void button1_Click(object sender, EventArgs e)//测试
{
m_cn = Text_CN.Text;
if (this.UserExists(m_cn))
{
LabelCheck.Text = "用户名已存在";
}
else
{
LabelCheck.Text = "用户名可用";
}
}
private void Login_Click(object sender, EventArgs e) //确定
{
m_cn = Text_CN.Text;
m_bs = Text_Bname.Text;
m_description = Text_Description.Text;
m_givename = Text_GiveName.Text;
m_mail = Text_Mail.Text;
m_pass1 = Text_Pass1.Text;
m_pass2 = Text_Pass2.Text;
m_sn = Text_SN.Text;
m_telephone = Text_Telephone.Text;
m_www = Text_WWW.Text;
if (UserExists(m_cn))
{
MessageBox.Show("用户名已存在!");
}
else if (m_pass1.Equals("") || m_pass2.Equals("") || !m_pass1.Equals(m_pass2))
{
MessageBox.Show("密码有误!");
}
else if (IsEmail(m_mail))
{
MessageBox.Show("邮箱地址格式不正确!");
}
else
{
try
{
DirectoryEntry myEntry = new DirectoryEntry(m_bs, m_uname, m_pword, AuthenticationTypes.Secure);
//"LDAP://192.168.0.169/OU=eZHR,DC=Lanall,DC=com", "域管理用户", "域管理用户密码", AuthenticationTypes.Secure);
DirectoryEntries myEntries = myEntry .Children;
string Strname = "CN=" + m_cn;
DirectoryEntry myDirectoryEntry = myEntries.Add(Strname, "user");
myDirectoryEntry.Properties["userPrincipalName"].Value = m_sn + m_givename;
myDirectoryEntry.Properties["name"].Value = m_givename;
myDirectoryEntry.Properties["samAccountName"].Value = m_cn;
myDirectoryEntry.Properties["pwdLastSet"].Value = -1;
myDirectoryEntry.Properties["userAccountControl"].Value = 553;//553;// 66048; //590336;
myDirectoryEntry.Properties["sn"].Value = m_sn;
myDirectoryEntry.Properties["givenName"].Value = m_givename;
myDirectoryEntry.Properties["telephoneNumber"].Value = m_telephone;
myDirectoryEntry.Properties["mail"].Value = m_mail;
myDirectoryEntry.Properties["wWWHomePage"].Value = m_www;
myDirectoryEntry.Properties["description"].Value = m_description;
myDirectoryEntry.CommitChanges();
myDirectoryEntry.Invoke("SetPassword", new object[] {m_pass1});
MessageBox.Show("添加成功!");
}
catch (Exception ex)
{
string str = ex.Message;
}
}
}
private void Del_Click(object sender, EventArgs e)//删除用户
{
m_cn = Text_CN.Text;
try
{
if (UserExists(m_cn))
{
DirectoryEntry entry = new DirectoryEntry(m_sname + m_bname, m_uname, m_pword);
DirectorySearcher Search = new DirectorySearcher();
Search.SearchRoot = entry;
Search.Filter = "(&(objectClass=user) (cn=" + m_cn + "))";
SearchResult Result = Search.FindOne();
DirectoryEntry child = Result.GetDirectoryEntry();
child.DeleteTree();
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("不存在用户:" + m_cn);
}
}
catch (Exception ex)
{
string Str = ex.Message;
}
}
- 大小: 25.2 KB
- 大小: 32.9 KB
分享到:
相关推荐
主要介绍了详解C#使用AD(Active Directory)验证内网用户名密码的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
活动目录 Active Directory 基于C# 源码 winform 想要基于WEB的,联系我吧.
When it comes to programmatically accessing Microsoft's Active Directory a lot of people seem to have quite a difficult time tying all the pieces together to accomplish exactly what they want to....
通过C#访问Active Directory对象(Visual Studio)
windows域管理网页版实现,支持部署到服务器,可以登录后进行用户、组、组织单元的增删改查及移动,本资源为源码,包括后端及页面的实现,使用vs发布后可直接部署
Windows Server 2008 R2 配置 AD(Active Directory)域控制器 配置环境 1配置 DNS 服务器 2.配置 Active Directory 域服务 3.C# AD(Active Directory)域同步 组织单位、用户基本信息
这一工具由Unosquare开发,采用C#语言编写,并利用ASP.NET Core框架与Material UI(React组件)精心打造用户界面。PassCore的一个显著优势是其跨平台兼容性:它不仅能够在Windows和Linux服务器上运行,而且还支持...
Active Directory对象选择器.NET的标准Active Directory对象选择器对话框 该项目基于由Armand du Plessis于2004年创建的 。 它已经更新,可以在64位Windows版本上使用。 现在,它适用于所有.Net框架版本,包括2.0、...
逻辑应用程序的Active Directory连接器 使用Azure Active Directory应用程序身份验证查询Graph API的API。特征API应用程序可让您查询广告租户: 获取所有用户的列表按公司名称获取所有用户的列表通过用户主体名称...
适用于.NET,Windows Store,.NET Core,Xamarin iOS和Xamarin Android的Active Directory身份验证库(ADAL) 立即更新到MSAL.NET! 是与Microsoft身份平台一起使用的新身份验证库 MSAL建立在ADAL之上,可与新的和...
ActiveDirectoryServices 具有更全面功能的API,用于连接和管理Active Directory(AD)和Lightweight Directory(AD-LDS)实例,并附带消费者应用程序示例。
本来想拿学校机房的Android编辑器直接粘到自己电脑上用,发现它的eclipse是32位的,而我的JDK是64位的,于是想到干脆装两个JDK,用C#做一个能够更改环境变量的程序 环境变量是包含关于系统及当前登录用户的环境信息...
Active Directory Service Interfaces (ADSI) 847 Programming Active Directory 848 Classes in System.DirectoryServices 849 Binding 849 Getting Directory Entries 854 Object Collections 855 Cache 857 ...
"If you have any interest in writing .NET programs using Active Directory or ADAM, this is the book you want to read." Joe Richards, Microsoft MVP, directory services Identity and Access Management ...
嗨,朋友们,我正在使用WSManConnectionInfo通过C#执行Powershell脚本,正在进行从本地计算机到Lync Server的远程连接,并执行以下命令。使用(Pipeline pipe = remoteRunspace.CreatePipeline()){Command ...
"If you have any interest in writing .NET programs using Active Directory or ADAM, this is the book you want to read." Joe Richards, Microsoft MVP, directory services Identity and Access Management ...
1、拿到户的凭据,能不能户的凭据来申请个证书,这个证书于 kerberos 2、能不能在户的电脑上找到证书,以后拿着这个证书去做认证 3、什么样的证书能于 ke
它可与任何LDAP协议兼容的目录服务器(包括Microsoft Active Directory)一起使用。 该库最初来自Novell( )-真正的旧代码库-看起来像Java的基于工具的转换-这是原始的Java代码库 (该存储库中的第一次提交来自...