`
- 浏览:
2113928 次
- 性别:
- 来自:
深圳
-
Active Directory如何用C#进行增加、删除、修改、查询用户与组织单位!
首先我们来了解一下什么是Active Directory。不用我描述,看以下网址,或在.net自带帮助文档里根据Active Directory关键字一搜,就什么都明白了。http://developer.ccidnet.com/pub/article/c322_a28703_p2.html接下来,我们来看看权限。你可以通过“网上邻居--整个网络--Directory--demain(你的域名)”你就可以看到所有关于域下的信息,粗一看就知道是怎么回事了。需要告诉大家的:所有组织单位下的用户都在Users(容器)--Demain Users(组)中用代码进行访问时,如果你是域管理员用户,则可以做任何操作,否则,只能查询用户属性。private void SearchUser(){string domainName = "Domain";string groupName = "Domain Users";string dirmemName="";//在Domain Users域用户里取得每个用户名System.DirectoryServices.DirectoryEntry group = new System.DirectoryServices.DirectoryEntry("WinNT://" + domainName + "/" + groupName + ",group");foreach(Object member in (IEnumerable)group.Invoke("Members")){//根据很个用户生成如:"LDAP://OU=套装软体课,OU=系统开发部,OU=资讯服务处,OU=营运支援中心,OU=XX公司,DC=Domain,DC=com,DC=cn"System.DirectoryServices.DirectoryEntry dirmem = new System.DirectoryServices.DirectoryEntry(member);dirmemName=dirmem.Name;string DomainName="Domain";string FilterStr = "(sAMAccountname="+dirmemName+")";System.DirectoryServices.DirectorySearcher FindMe = new System.DirectoryServices.DirectorySearcher(DomainName);FindMe.Filter = FilterStr;System.DirectoryServices.SearchResult FindRes = FindMe.FindOne();System.DirectoryServices.DirectoryEntry MyUser = FindRes.GetDirectoryEntry();string OUPath=MyUser.Parent.Path;//找到该用户所在的LDAP:后,由域管理员登录,并取得该用户的所在属性。string strFieldsValue="",strFields="";System.DirectoryServices.DirectoryEntry myds=new System.DirectoryServices.DirectoryEntry(OUPath,"域管理员名","域管理员密码");foreach(System.DirectoryServices.DirectoryEntry tempEntry in myds.Children){if(tempEntry.SchemaClassName.ToString() == "user" && tempEntry.Properties["sAMAccountName"].Value.ToString().ToLower()==dirmemName){foreach (string propertyName in tempEntry.Properties.PropertyNames ){string oneNode = propertyName + ": " +entry.Properties[propertyName][0].ToString();this.Textbox1.Text=oneNode;}}
public void AddUser(string strPath,string Username,string ChineseName)//strPath 增加用户至哪个组织单位如"LDAP://OU=XX公司,DC=Domain,DC=com"帐号、中文名{try{string RootDSE;//System.DirectoryServices.DirectorySearcher DSESearcher= new System.DirectoryServices.DirectorySearcher();//RootDSE=DSESearcher.SearchRoot.Path;//RootDSE="LDAP://DC=Domain,DC=com";//RootDSE=RootDSE.Insert(7,"CN=Users,");System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(strPath); System.DirectoryServices.DirectoryEntries myEntries =
myDE.Children;// Create a new entry 'Sample' in the container.string strname="CN="+ChineseName;System.DirectoryServices.DirectoryEntry myDirectoryEntry = myEntries.Add(strname, "user");//MessageBox.Show(myDirectoryEntry.SchemaClassName.ToString());myDirectoryEntry.Properties["userPrincipalName"].Value=Username;myDirectoryEntry.Properties["name"].Value=ChineseName;myDirectoryEntry.Properties["samAccountName"].Value=Username;myDirectoryEntry.Properties["userAccountControl"].Value =66048; //590336;myDirectoryEntry.CommitChanges();}
private void addOU(string strPath,string OUName)//增加组织到strPath组织单位下,组织名称{try{//String RootDSE;//System.DirectoryServices.DirectorySearcher DSESearcher= new System.DirectoryServices.DirectorySearcher();//RootDSE=DSESearcher.SearchRoot.Path;//RootDSE="LDAP://OU=百意时尚广场,DC=Domain,DC=com";System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(strPath);System.DirectoryServices.DirectoryEntries myEntries = myDE.Children;string name="OU="+OUName;System.DirectoryServices.DirectoryEntry myDirectoryEntry = myEntries.Add(name,"organizationalUnit");myDirectoryEntry.Properties["name"].Value=OUName;myDirectoryEntry.Properties["instanceType"].Value=4;myDirectoryEntry.Properties["distinguishedName"].Value="OU="+OUName+",DC=Domain,DC=COM)";myDirectoryEntry.Properties["objectCategory"].Value="CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=sedep,DC=COM";myDirectoryEntry.Properties["ou"].Value=OUName;myDirectoryEntry.Properties["postalCode"].Value="777";myDirectoryEntry.CommitChanges();//UserMoveto("LDAP://OU="+OUName+",DC=sedep,DC=com",strPath);}catch(Exception RaiseErr){MessageBox.Show (RaiseErr.Message);}}
private void ModifyUser(){try{string DomainName="Domain";string FilterStr = "(sAMAccountname=karlluo)";System.DirectoryServices.DirectorySearcher FindMe = new System.DirectoryServices.DirectorySearcher(DomainName);FindMe.Filter = FilterStr;System.DirectoryServices.SearchResult FindRes = FindMe.FindOne();string tt=FindRes.Path;System.DirectoryServices.DirectoryEntry MyUser = FindRes.GetDirectoryEntry();string OUPath=MyUser.Parent.Path;DirectoryEntry myds=new DirectoryEntry(OUPath,"域管理员名","域管理员密码");foreach(System.DirectoryServices.DirectoryEntry tempEntry in myds.Children){if(tempEntry.SchemaClassName.ToString() == "user"){if(tempEntry.Properties["sAMAccountName"].Value.ToString().ToLower()=="karlluo"){tempEntry.UsePropertyCache=true;tempEntry.Properties["st"].Value="yyyyyyyyyyyyyyyy";//newEntry.Properties["userPrincipalName"].Value="userID";tempEntry.CommitChanges();}}}}catch(Exception RaiseErr){MessageBox.Show (RaiseErr.Message);}}很多网友都需要AD同步的代码,现在共享一下,免得大家都找我要,AD同步示例
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
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#使用AD(Active Directory)验证内网用户名密码的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
windows域管理网页版实现,支持部署到服务器,可以登录后进行用户、组、组织单元的增删改查及移动,本资源为源码,包括后端及页面的实现,使用vs发布后可直接部署
通过C#访问Active Directory对象(Visual Studio)
Windows Server 2008 R2 配置 AD(Active Directory)域控制器 配置环境 1配置 DNS 服务器 2.配置 Active Directory 域服务 3.C# AD(Active Directory)域同步 组织单位、用户基本信息
活动目录 Active Directory 基于C# 源码 winform 想要基于WEB的,联系我吧.
这一工具由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应用程序可让您查询广告租户: 获取所有用户的列表按公司名称获取所有用户的列表通过用户主体名称...
LINQ提供程序基于System.DirectoryServices.Protocols构建,用于与LDAP服务器进行交互。 概述 关于如何通过System.DirectoryServices和System.DirectoryServices.AccountManagement访问Active Directory的示例很多。...
适用于.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)实例,并附带消费者应用程序示例。
是英文的,全面包含了c#的各方面。是非常好的一本书,绝对值得下载。 Introduction xxvii Part I: The C# Language 1 Chapter 1: .NET Architecture 3 The Relationship of C# to .NET 4 The Common Language ...
这是针对Active Directory的c#WinForm开发资源的好文收藏,欢迎交流共享
"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 ...
单一登录(又名SSO,又名集成登录),用于与Active Directory进行对话。 您可以使用此应用程序让您的用户使用其Windows帐户登录,而无需输入密码。 感谢提供了SSO代码。 部署 您可以使用Visual Studio进行部署,...