public ActionResult Login(Model.LoginModel model, string returnUrl) { var IsLogin = ""; try { if (ModelState.IsValid) { string[] SplitAccount = new string[] { }; var username = ""; Entity.advt_users users = new advt_users(); Regex RegEmail = new Regex(@"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 Match m = RegEmail.Match(model.UserName); //工号 var wuser = Data.ExamUsersFromehr.Get_ExamUsersFromehr(new { UserCode = model.UserName }); if (wuser != null) { Service.IProvider.IAuthorizationServices services = new Service.Provider.AuthorizationServices(); users = services.EmailAuthenticate(wuser.CommpanyEmail, model.Password); if (users != null) //验证通过 { username = wuser.EamilUsername; } else { IsLogin = "******"; } } var cuser = Data.ExamUsersFromehr.Get_ExamUsersFromehr(new { EamilUsername = model.UserName }); if (cuser != null) { var acc = "acn\\" + cuser.EamilUsername.Trim(); SplitAccount = acc.Split('\\'); username = cuser.EamilUsername; } if (cuser != null) { if (SplitAccount.Length > 1) { String adPath = ""; //Fully-qualified Domain Name switch (SplitAccount[0].ToLower().Trim()) { case "acn": adPath = "LDAP://acn.advantech.corp"; //acn break; case "aeu": adPath = "LDAP://aeu.advantech.corp"; //advantech break; case "aus": adPath = "LDAP://aus.advantech.corp"; //advantech break; case "advantech": adPath = "LDAP://advantech.corp"; //advantech break; default: adPath = "LDAP://acn.advantech.corp"; //acn break; } LdapAuthentication adAuth = new LdapAuthentication(adPath); string password = model.Password.Trim(); if (true == adAuth.IsAuthenticated(SplitAccount[0], SplitAccount[1], model.Password)) { Service.IProvider.IAuthorizationServices service = new Service.Provider.AuthorizationServices(); users = service.Authenticate(username, model.Password); } else { IsLogin = "******"; } } } if (wuser == null && cuser == null) { IsLogin = "******"; } if (string.IsNullOrEmpty(IsLogin) && !string.IsNullOrEmpty(users.username)) { SetUserAuthIn(users.username.ToString(), users.password, string.Empty, false); //写入Cookie,无需登入。 var LF = Guid.NewGuid().ToString(); //写内存 Manager.Login.Lock_Flag = LF; //写本地 Utils.WriteCookie("ALock", LF); users.msn = LF; advt.Data.advt_users.Update_advt_users(users, null, new string[] { "id" }); XUtils.WriteUserCookie(users, model.CookieTime ?? 0, Config.BaseConfigs.Passwordkey, 1); IsLogin = "******"; } } } catch (Exception ex) { return(Json(new { IsLogin = ex.Message }, JsonRequestBehavior.AllowGet)); throw; } //ModelState.AddModelError("", "用户名或者密码错误!"); return(Json(new { IsLogin }, JsonRequestBehavior.AllowGet)); }
public ActionResult ChangePassword(int?id, Model.LocalPasswordModel model) { Entity.advt_users info = new advt_users(); if (UserContext.roles == (int)Entity.Status.RoleStatus.Admin) { if (id != null) { info = Data.advt_users.Get_advt_users(id ?? 0); } else { info = UserContext; } } else { info = UserContext; } if (info == null) { ModelState.AddModelError("", "系统错误!"); } if (ModelState.IsValid) { ViewBag.Userinfo = info; ViewBag.id = info.id; bool changePasswordSucceeded = false; try { if (ModelState.IsValid) { string pwd = Utils.MD5(model.OldPassword); if (UserContext.roles == (int)Entity.Status.RoleStatus.Admin || info.password == pwd) { info.password = Utils.MD5(model.NewPassword); int result_i = Data.advt_users.Update_advt_users(info, null, new string[] { "id" }); changePasswordSucceeded = result_i == 1; } } } catch (Exception) { changePasswordSucceeded = false; } if (changePasswordSucceeded) { XUtils.WriteUserCookie(info, 0, Config.BaseConfigs.Passwordkey, 1); right("修改密码成功!"); } else { ModelState.AddModelError("", "当前的密码是不正确的或新的密码是无效的!"); } } return(View(model)); }