.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();
}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM