.Net C#域帳號登錄驗證

//獲取登錄頁輸入的域帳號、密碼
string domainIP = "";      //域IP
string userAccount = "";    //域帳號
string Password = "";      //域帳號密碼          
using (DirectoryEntry deUser = new DirectoryEntry(@"LDAP://" + domainIP, userAccount, Password))
{
DirectorySearcher src = new DirectorySearcher(deUser);
src.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + userAccount + "))";
src.PropertiesToLoad.Add("cn");
src.SearchRoot = deUser;
src.SearchScope = SearchScope.Subtree;

SearchResult result = src.FindOne();
if (result != null)//驗證成功
{
DirectoryEntry de = result.GetDirectoryEntry();
string userID = de.Username;
#region 域帳號驗證經過後判斷是否用戶在本系統中
var existUser = new DAL.Maintain.UserOP().GetData(userID);
if (existUser == null)
{
ViewBag.ErrorInfo = "提示:您未開通系統的權限,請聯繫管理員。<br/>You have no access to the system, please contact the administrator!";
return View();
} 
#endregion

FormsAuthentication.SetAuthCookie(userID, false);
Session.Add("UserID", userID);
Logger.DefaultLog.Info("登錄sessionUserID:" + Session["UserID"].ToString());

if (string.IsNullOrEmpty(ReturnUrl))
{
return Redirect(Url.Action("Index", "Home"));
}
else
{
//...
}
}
else
{
ViewBag.ErrorInfo = "提示:登陸失敗: 未知的用戶名或錯誤密碼。<br/>Account or Password is incorrect!";
return View();
}

}
相關文章
相關標籤/搜索