標籤:des class tar ext c get
ASP.NET串連LDAP資料庫的有關資訊
一、封裝在DAL層中的擷取使用者資訊的函數
/// <summary>
/// 按照使用者Id尋找使用者資訊
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
publicDirectoryEntry GetUser(string username)
{
string path = System.Configuration.ConfigurationManager.ConnectionStrings["path"].ConnectionString;
string pname = System.Configuration.ConfigurationManager.ConnectionStrings["pname"].ConnectionString;
string pwd = System.Configuration.ConfigurationManager.ConnectionStrings["pwd"].ConnectionString;
// 3個串連資料庫的資訊寫在設定檔裡
DirectoryEntry deuser; //定義變數
try
{
DirectoryEntry de = newDirectoryEntry (path, pname, pwd, AuthenticationTypes.Secure);
DirectorySearcher deSearch = newDirectorySearcher(de); //串連LDAP資料庫
deSearch.Filter = "(&(objectClass=userinfo)(LOGINNAME=" + username + "))"; //篩選比對
//上面這句話修改為:mySearcher.Filter = "(&(objectClass=userinfo)(&(LOGINNAME=" + txtUserId.Text + ")(LOGINPASSWORD=" + txtUserPwd.Text + ")))";//作為登入是使用者帳號和密碼認證
deSearch.SearchScope = SearchScope.Subtree;
SearchResult result = deSearch.FindOne(); //篩選比對得出一個結果,儲存在result中
if (result != null)
{
deuser = result.GetDirectoryEntry(); //得到篩選結果,並賦值給deuser中
return deuser;
}
else
{
returnnull;
}
}
catch(Exception ex)
{
// LogManage.SaveInfo(ex.ToString());
returnnull;
}
二、設定檔資訊
<connectionStrings>
<add name="path"
connectionString="LDAP://192.168.1.1/OU=123,DC=123,DC=COM" />
<add name="pname"
connectionString="123" />
<add name="pwd"
connectionString="123" />
</connectionStrings>
三、實現層
1、頁面資訊
使用者id:(textbox框)
使用者名稱稱:(textbox框)
使用者密碼:(textbox框)
電子郵箱:(textbox框)
ButtonID="butquchu"(讀取資料按鈕)
2、事件函數代碼
protectedvoid butquchu_Click(object sender, EventArgs e)
{
ldapDAO ld= newldapDAO ();
string username = Session["LOGINNAME"].ToString(); //根據上一頁的登入資訊擷取使用者帳號,儲存在session中。
labname.Text = username;
DirectoryEntry de = ld.GetUser(username); //調用ldapDAO中的擷取使用者資訊函數
if (de != null)
{
if (de != null)
{
if (de.Properties["USERID"].Value != null)
{
txtuserid.Text = de.Properties["USERID"].Value.ToString();
}
if (de.Properties["LOGINNAME"].Value != null)
{
txtusername.Text = de.Properties["LOGINNAME"].Value.ToString();
}
if (de.Properties["LOGINPASSWORD"].Value != null)
{
txtpwd.Text = de.Properties["LOGINPASSWORD"].Value.ToString();
}
if (de.Properties["EMAIL"].Value != null)
{
txtmail.Text = de.Properties["EMAIL"].Value.ToString();
}
}