public bool AuthDomainUserByAdHelper(string username, string domain, string passwd) { // 获取配置信息 var config = BLL.SystemConfigBll.GetConfig(); if (config != null && !string.IsNullOrEmpty(config.Domin) && !string.IsNullOrEmpty(config.DominAdminName)) { // 获取管理员信息 var adminUserInfo = new ManagerBll().GetModel(config.DominAdminName); if (adminUserInfo != null && !string.IsNullOrEmpty(adminUserInfo.UserName) && !string.IsNullOrEmpty(adminUserInfo.Password)) { Bll.ADHelper.SetADValue(config.Domin, adminUserInfo.UserName, adminUserInfo.Password); ADHelper.LoginResult result = Bll.ADHelper.LoginByAccount(username, passwd); if (result == ADHelper.LoginResult.LOGIN_USER_OK) { return(true); } else { result = Bll.ADHelper.Login(username, passwd); return(result == ADHelper.LoginResult.LOGIN_USER_OK); } } } return(false); }
protected void btLogin_Click(object sender, EventArgs e) { MDUserLoginData oMDUser = new MDUserLoginData(); try { if (CheckLogin()) { string username = txtUsername.Text.Trim(); string password = txtPassword.Text.Trim(); ADHelper.LoginResult lgresr = new ADHelper.LoginResult(); lgresr = adHl.Login(username, password); if (lgresr == ADHelper.LoginResult.LOGIN_OK) { DataSet usr = adHl.GetUserDataSet(username); DataTable dtUser = new DataTable(); dtUser = usr.Tables["User"]; string EmpCode = dtUser.Rows[0]["Zip"].ToString(); string email = dtUser.Rows[0]["EmailAddress"].ToString(); // Login Success oMDUser = oUser.LoginSystem(EmpCode); Session["UserLoginData"] = oMDUser; Session["EmpCode"] = EmpCode; Session["UserName"] = username; Session["UserFullName"] = dtUser.Rows[0]["LoginName"].ToString(); if (ViewState["PathURL"] != "") { Response.Redirect(ResolveClientUrl("~/" + ViewState["PathURL"].ToString())); } else { Response.Redirect(ResolveClientUrl("~/Default.aspx")); } } else { string CheckUserInSystem = "false"; CheckUserInSystem = oUser.CheckUserInSystem(txtUsername.Text.Trim()); if (Convert.ToBoolean(CheckUserInSystem)) { bool CheckUserHavePassword = false; CheckUserHavePassword = oUser.CheckUserHavePassword(txtUsername.Text.Trim()); if (CheckUserHavePassword) { oMDUser = oUser.LoginSystem(txtUsername.Text.Trim(), EncryptPassword(txtPassword.Text.Trim())); Session["UserLoginData"] = oMDUser; Session["UserName"] = oMDUser.ListOfUser[0].empCode; Session["UserFullName"] = oMDUser.ListOfUser[0].firstname + " " + oMDUser.ListOfUser[0].lastname; if (ViewState["PathURL"] != "") { string PathURL = "~/" + ViewState["PathURL"].ToString(); Response.Redirect(ResolveClientUrl(PathURL)); } else { Response.Redirect(ResolveClientUrl("~/Default.aspx")); } } else { bool LoginSuccess = false; using (PrincipalContext pc = new PrincipalContext(ContextType.Domain)) { if (pc.ValidateCredentials(txtUsername.Text.Trim(), txtPassword.Text.Trim())) { // Login With AD Server LoginSuccess = true; } } if (LoginSuccess) { // Login Success oMDUser = oUser.LoginSystem(txtUsername.Text.Trim()); Session["UserLoginData"] = oMDUser; Session["UserName"] = oMDUser.ListOfUser[0].empCode; Session["UserFullName"] = oMDUser.ListOfUser[0].fullname; if (ViewState["PathURL"] != "") { Response.Redirect(ResolveClientUrl("~/" + ViewState["PathURL"].ToString())); } else { Response.Redirect(ResolveClientUrl("~/Default.aspx")); } } else { txtUsername.BackColor = Color.Pink; txtPassword.BackColor = Color.Pink; lbError.Text = "Username or Password incorrect!"; txtPassword.Focus(); } } } else { txtUsername.BackColor = Color.Pink; txtPassword.BackColor = Color.Pink; lbError.Text = "Username or Password incorrect!"; txtPassword.Focus(); } } } } catch (Exception) { } }