asp.net讀取Windows域(AD)資訊
class Domain
{
public string IsUserExistInDomain(string ouInfo, string domainAdmin, string domainPwd)
{
Write_log.WriteLog("開始");
string strOUInfo = ouInfo;
string strDomainAdmin = domainAdmin;
string strDomainPassword = domainPwd;
string strDomainIP = ConfigurationManager.AppSettings["DomainIP"].ToString().Trim();
string strTemp = "LDAP://" + strDomainIP.Trim() + "/" + strOUInfo.Trim();
DirectoryEntry objDE = new DirectoryEntry(strTemp, strDomainAdmin.Trim(), strDomainPassword.Trim());
string strFilter = "(&(objectCategory=organizationalUnit)(objectClass=organizationalUnit))";
DirectorySearcher objSearcher = new DirectorySearcher(objDE, strFilter);
//objSearcher.Sort = new SortOption("name", SortDirection.Ascending);
SearchResultCollection src = null;
try
{
src = objSearcher.FindAll();//判斷域是否連線成功
}
catch (Exception err)
{
MessageBox.Show("LDAP,帳號,密碼出錯,請重新輸入!" + err.Message);
return "-1";
}
OleDbTransaction tran = null;
OleDbCommand cmd = null;
OleDbConnection conn = new OleDbConnection();
string connStr = new DBHelper().ConnectionString("bpm");
string strLFAccount = string.Empty;
string hrID = string.Empty;
try
{
conn.ConnectionString = connStr;
conn.Open();
tran = conn.BeginTransaction();
cmd = conn.CreateCommand();
cmd.Transaction = tran;
DeleteADUsers(cmd);//delete all adusers that in bpm_AD_USER table,該方法不存在,但不影響對於域操作的理解
{
public string IsUserExistInDomain(string ouInfo, string domainAdmin, string domainPwd)
{
Write_log.WriteLog("開始");
string strOUInfo = ouInfo;
string strDomainAdmin = domainAdmin;
string strDomainPassword = domainPwd;
string strDomainIP = ConfigurationManager.AppSettings["DomainIP"].ToString().Trim();
string strTemp = "LDAP://" + strDomainIP.Trim() + "/" + strOUInfo.Trim();
DirectoryEntry objDE = new DirectoryEntry(strTemp, strDomainAdmin.Trim(), strDomainPassword.Trim());
string strFilter = "(&(objectCategory=organizationalUnit)(objectClass=organizationalUnit))";
DirectorySearcher objSearcher = new DirectorySearcher(objDE, strFilter);
//objSearcher.Sort = new SortOption("name", SortDirection.Ascending);
SearchResultCollection src = null;
try
{
src = objSearcher.FindAll();//判斷域是否連線成功
}
catch (Exception err)
{
MessageBox.Show("LDAP,帳號,密碼出錯,請重新輸入!" + err.Message);
return "-1";
}
OleDbTransaction tran = null;
OleDbCommand cmd = null;
OleDbConnection conn = new OleDbConnection();
string connStr = new DBHelper().ConnectionString("bpm");
string strLFAccount = string.Empty;
string hrID = string.Empty;
try
{
conn.ConnectionString = connStr;
conn.Open();
tran = conn.BeginTransaction();
cmd = conn.CreateCommand();
cmd.Transaction = tran;
DeleteADUsers(cmd);//delete all adusers that in bpm_AD_USER table,該方法不存在,但不影響對於域操作的理解
// 對於域資訊的操作,有些方法暫時沒有提供,但不影響對於域操作的理解
foreach (SearchResult sr in src)
{
string strOrgName = sr.Properties["name"][0].ToString();
DirectoryEntry de = sr.GetDirectoryEntry();
foreach (DirectoryEntry child in de.Children)
{
if (child.SchemaClassName.Contains("user"))
{
if (null == child.Properties["description"].Value || "" == child.Properties["description"].Value.ToString().Trim())
{
continue;
}
if (null == child.Properties["userprincipalname"].Value || "" == child.Properties["userprincipalname"].Value.ToString().Trim())
{
continue;
}
hrID = child.Properties["description"].Value.ToString().Trim();
//使用者的ou名稱
string ouName = child.Name.Split(new char[] { '=' })[1].ToString();
//使用者的帳號
if (ouName.Contains("\\"))
{
string start = ouName.Split(new char[] { '\\' })[0].ToString();
string end = ouName.Split(new char[] { '\\' })[1].ToString().Substring(1);
ouName = start + "_" + end;
}
strLFAccount = common.strLiveflowName(child.Properties["userprincipalname"].Value.ToString().Trim()).ToLower();
string USER_Mb = "";
string USER_Tel = "";
string USER_Email = "";
if (child.Properties["mail"].Value != null)
{
USER_Email = child.Properties["mail"].Value.ToString();
}
if (child.Properties["mobile"].Value != null)
{
USER_Mb = child.Properties["mobile"].Value.ToString();
}
if (child.Properties["telephonenumber"].Value != null)
{
USER_Tel = child.Properties["telephonenumber"].Value.ToString();
}
InsertADUsers(cmd,strLFAccount,hrID,ouName,USER_Email,USER_Mb,USER_Tel);
}
}
}
tran.Commit();
}
catch
{
if (tran.Connection.State == ConnectionState.Open)
{
tran.Rollback();
}
throw;
}
finally
{
conn.Close();
conn.Dispose();
}
//ArrayList listADOrgCode = new ArrayList();
Write_log.WriteLog("結束");
// MessageBox.Show("域使用者同步完成");
return strLFAccount;
}
}
foreach (SearchResult sr in src)
{
string strOrgName = sr.Properties["name"][0].ToString();
DirectoryEntry de = sr.GetDirectoryEntry();
foreach (DirectoryEntry child in de.Children)
{
if (child.SchemaClassName.Contains("user"))
{
if (null == child.Properties["description"].Value || "" == child.Properties["description"].Value.ToString().Trim())
{
continue;
}
if (null == child.Properties["userprincipalname"].Value || "" == child.Properties["userprincipalname"].Value.ToString().Trim())
{
continue;
}
hrID = child.Properties["description"].Value.ToString().Trim();
//使用者的ou名稱
string ouName = child.Name.Split(new char[] { '=' })[1].ToString();
//使用者的帳號
if (ouName.Contains("\\"))
{
string start = ouName.Split(new char[] { '\\' })[0].ToString();
string end = ouName.Split(new char[] { '\\' })[1].ToString().Substring(1);
ouName = start + "_" + end;
}
strLFAccount = common.strLiveflowName(child.Properties["userprincipalname"].Value.ToString().Trim()).ToLower();
string USER_Mb = "";
string USER_Tel = "";
string USER_Email = "";
if (child.Properties["mail"].Value != null)
{
USER_Email = child.Properties["mail"].Value.ToString();
}
if (child.Properties["mobile"].Value != null)
{
USER_Mb = child.Properties["mobile"].Value.ToString();
}
if (child.Properties["telephonenumber"].Value != null)
{
USER_Tel = child.Properties["telephonenumber"].Value.ToString();
}
InsertADUsers(cmd,strLFAccount,hrID,ouName,USER_Email,USER_Mb,USER_Tel);
}
}
}
tran.Commit();
}
catch
{
if (tran.Connection.State == ConnectionState.Open)
{
tran.Rollback();
}
throw;
}
finally
{
conn.Close();
conn.Dispose();
}
//ArrayList listADOrgCode = new ArrayList();
Write_log.WriteLog("結束");
// MessageBox.Show("域使用者同步完成");
return strLFAccount;
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-624905/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Asp.net Core 和類庫讀取配置檔案資訊ASP.NET
- .net讀取Windows登入使用者資訊(downmoon)Windows
- Windows mobile 下讀取手機SIM卡資訊Windows
- 關於Asp.net core配置資訊讀取的原始碼分析梳理ASP.NET原始碼
- ASP.Net 獲取伺服器資訊ASP.NET伺服器
- asp.net獲取伺服器資訊ASP.NET伺服器
- Ubuntu 加入Windows AD域步驟UbuntuWindows
- angular中$location讀取url資訊Angular
- C#讀取EXIF資訊類C#
- 因為跨域問題導致的無法讀取 response header跨域Header
- ASP.NET Core 中的檔案提供程式 遍歷資料夾讀取檔案資訊ASP.NET
- Java 建立、填充、讀取PDF表單域Java
- PowerShell讀取Windows產品金鑰Windows
- ASP.NET Core - 配置系統之配置讀取ASP.NET
- Windows Phone 7 SIM卡資訊獲取Windows
- vmi:獲取 windows 系統硬體資訊Windows
- 組策略中的稽核策略提示 Windows無法讀取模板資訊的解決方法Windows
- asp.net獲取web.config配置資訊ASP.NETWeb
- java讀取(華視)刷卡機的刷卡資訊Java
- spring boot(二)配置資訊的讀取Spring Boot
- SpringBoot 複雜配置資訊讀取Spring Boot
- Python讀取Ansible playbooks返回資訊Python
- (slam工具)2 從照片讀取GNSS資訊SLAM
- C# 獲取客戶端資訊 /asp.net/WebService/WebFormC#客戶端ASP.NETWebORM
- SaxReader讀取,更新xml檔案XML
- PHP讀取Excel之Spreadsheet_Excel_ReaderPHPExcel
- C語言/C++讀取檔案資訊C語言C++
- ColdFusion如何從資料庫讀取資訊例子資料庫
- 讀取麒麟系統的各項版本資訊
- asp.net web api 2 host in a windows service推薦閱讀ASP.NETWebAPIWindows
- windows下讀取Linux分割槽軟體WindowsLinux
- Windows程式讀取不了中文路徑問題Windows
- Windows系統安全獲取重要資訊的方法(一)Windows
- 跨程式共享資料,讀取聯絡人資訊
- Android NFC的初次使用——公交卡資訊讀取Android
- Sql server:從XML檔案中讀取配置資訊SQLServerXML
- ASP.NET Core通過Nacos SDK讀取阿里雲ACMASP.NET阿里ACM
- 由ASP.NET Core讀取Response.Body引發的思考ASP.NET