Active Directory 域帐号审计程序
来源:互联网 发布:江苏省高考报名端口 编辑:程序博客网 时间:2024/05/21 19:39
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using DS= System.DirectoryServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private static string[] Propers = {"st",
"cn",
"mailnickname",
"postalcode",
"employeeid",
"c",
"memberof",
"displayname",
"streetaddress",
"whencreated",
"l",
"co",
"physicaldeliveryofficename",
"samaccountname",
"mail",
"mobile",
"name",
"department",
"userprincipalname",
"telephonenumber",
"company",
"useraccountcontrol" };
private string ReportFile{get; set;}
private int FinishedRecord { get; set; }
private string username {get; set;}
private string password {get; set;}
private string hostname {get; set;}
protected string schemaNamingContent {get; set;}
protected string defaultNamingContent {get; set;}
public Form1()
{
InitializeComponent();
}
private void LoadtheProperties_Click(object sender, EventArgs e)
{
try
{
SetLogonInformation();
SetNamingContext();
SetUserProperties(schemaNamingContent);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
public event Finished ReportFinished;
public event ErrorHappend ErrorEvent;
public event ContinueProcess Processing;
public delegate void ErrorHappend(string ErrorMsg);
public delegate void Finished();
public delegate void ContinueProcess(int nRecordCount);
protected void SetLogonInformation()
{
username = (textBoxUsername.Text == "" ? null : textBoxUsername.Text);
password = (textBoxPassword.Text == "" ? null : textBoxPassword.Text);
hostname = textBoxHostName.Text;
if (hostname != "")
{
hostname += "/";
}
}
protected void SetNamingContext()
{
using (DirectoryEntry de = new DirectoryEntry())
{
string path = "LDAP://" + hostname + "rootDSE";
de.Username = username;
de.Password = password;
de.Path = path;
schemaNamingContent = de.Properties["schemaNamingContext"][0].ToString();
defaultNamingContent = de.Properties["defaultNamingContext"][0].ToString();
}
}
protected void SetUserProperties(string schemaNamingContext)
{
List<string> properties = new List<string>();
string[] data = GetSchemaProperties(schemaNamingContext, "User");
properties.AddRange(GetSchemaProperties(schemaNamingContext, "Organizational-Person"));
properties.AddRange(GetSchemaProperties(schemaNamingContext, "Person"));
properties.AddRange(GetSchemaProperties(schemaNamingContext, "TOP"));
}
protected string[] GetSchemaProperties(string schemaNamingContext, string objectType)
{
string[] data;
using (DirectoryEntry de = new DirectoryEntry())
{
de.Username = username;
de.Password = password;
de.Path = "LDAP://" + hostname + "CN=" + objectType + "," + schemaNamingContext;
DS.PropertyCollection properties=de.Properties;
DS.PropertyValueCollection values = properties["systemMayContain"];
data = new string[values.Count];
values.CopyTo(data, 0);
return data;
}
}
private void button2_Click(object sender, EventArgs e)
{
this.FinishedRecord = 0;
this.progressBar1.Value = 0;
try
{
SetLogonInformation();
SetNamingContext();
SetUserProperties(schemaNamingContent);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
this.saveFileDialog1.ShowDialog();
if (string.IsNullOrEmpty(saveFileDialog1.FileName))
{
return;
}
ReportFile = this.saveFileDialog1.FileName;
System.Threading.Thread oThread = new System.Threading.Thread(new System.Threading.ThreadStart(FillResult));
oThread.Start();
}
void Form1_ErrorEvent(string ErrorMsg)
{
System.Windows.Forms.MessageBox.Show(ErrorMsg);
}
void Form1_ReportFinished()
{
this.progressBar1.Value = this.progressBar1.Maximum;
System.Windows.Forms.MessageBox.Show("Export Finished");
}
void Form1_Processing(int nRecordCount)
{
if (nRecordCount < this.progressBar1.Maximum-10)
{
this.progressBar1.Value = nRecordCount;
}
}
private string GetHead()
{
StringBuilder summary = new StringBuilder();
foreach (string propName in Propers)
{
summary.Append("/"" + propName + "/",");
}
return summary.ToString().TrimEnd(',')+"/r/n";
}
private string GetRecord(SearchResult result)
{
StringBuilder summary = new StringBuilder();
foreach (string propName in Propers)
{
if (result.Properties[propName].Count == 0)
{
summary.Append("/"/",");
continue;
}
try
{
if (propName == "useraccountcontrol")
{
int oI = (int)result.Properties[propName][0];
if (oI == 514 || oI == 546 || oI == 66050)
{
summary.Append("/"Disable/",");
}
else
{
summary.Append("/"Enable/",");
}
}
else
{
if (propName == "whencreated")
{
summary.Append("/"" + ((System.DateTime)result.Properties[propName][0]).ToString() + "/",");
}
else
{
summary.Append("/"");
foreach (string s in result.Properties[propName])
{
if (s == "mobile"||s=="telephonenumber")
{
summary.Append("'"+s);
}
else
{
summary.Append(s);
}
}
summary.Append("/",");
}
}
}
catch(System.Exception oError)
{
System.Windows.Forms.MessageBox.Show(oError.Message);
return "";
}
}
return summary.ToString().TrimEnd(',');
}
protected void FillResult()
{
using (DirectoryEntry root = new DirectoryEntry())
{
root.Username = username;
root.Password = password;
root.Path = "LDAP://" + hostname + defaultNamingContent;
using (DirectorySearcher searcher = new DirectorySearcher())
{
searcher.SearchRoot = root;
searcher.SearchScope = SearchScope.Subtree;
if (this.checkUser.Checked)
{
searcher.Filter = "(objectCategory=user)";
}
else
{
searcher.Filter = "(objectCategory=group)";
}
searcher.PropertiesToLoad.AddRange(Propers);
SearchResultCollection results = null;
try
{
results = searcher.FindAll();
}
catch(System.Exception oError)
{
if (this.ErrorEvent != null)
{
this.Invoke(this.ErrorEvent, oError.Message);
}
return;
}
StringBuilder summary = new StringBuilder();
summary.Append(GetHead());
foreach (SearchResult result in results)
{
if (Processing != null)
{
Invoke(Processing, this.FinishedRecord++);
}
summary.Append( GetRecord(result)+"/r/n");
continue;
}
try
{
System.IO.File.WriteAllText(ReportFile, summary.ToString());
}
catch (System.Exception oError)
{
if (this.ErrorEvent != null)
{
this.Invoke(this.ErrorEvent, oError.Message);
}
}
if (this.ReportFinished != null)
{
Invoke(ReportFinished, null);
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.Processing += new ContinueProcess(Form1_Processing);
this.ReportFinished += new Finished(Form1_ReportFinished);
this.ErrorEvent += new ErrorHappend(Form1_ErrorEvent);
}
}
}
- Active Directory 域帐号审计程序
- 批量添加Active Directory帐号
- Active Directory帐号ID问题
- 批量创建Active Directory帐号
- 设置Active Directory域
- 设置 Active Directory 域
- 通过 Active Directory 管理自定义外壳程序
- Windows域 Active Directory介绍
- Active Directory 域服务 - 用户
- Active Directory
- Active Directory
- Active Directory
- 配置 Active Directory 域基础结构
- 如何设置Active Directory域控制器
- Active Directory(活动目录) & Domain(域)
- Active Directory之额外域控制器
- Active Directory---部署额外域控制器
- Active Directory Rights Management Services (AD RMS) 无法查询 Active Directory 网域服务 (AD DS)
- ASP.NET学习笔记
- Ping 编程
- liunx-gdb调试简要说明
- org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V
- 严重: action: null java.lang.ClassNotFoundException: org.springframework.web.struts.ContextLoaderPlugIn
- Active Directory 域帐号审计程序
- Microsoft VBScript 运行时错误代码大全
- 腹腰部该如何锻炼?
- Ping检测网络方法
- 疑问:如果你们之间没有过什么?那她又是如何坚定相信你出国回来念书是为了她?
- 让IIS支持任意扩展名和未知扩展名的下载
- Discriminative vs. Generative model
- 钩子程序
- 搞笑句子