public ActionResult LoginSSO(string email = "", string username = "", string oapass = "") { Session.Clear(); try { if (string.IsNullOrWhiteSpace(oapass)) { return(RedirectToAction("Index", "Login")); } if (string.IsNullOrWhiteSpace(email) && string.IsNullOrWhiteSpace(username)) { return(RedirectToAction("Index", "Login")); } var user = new SysUser(); if (!string.IsNullOrWhiteSpace(email)) { user = _userManager.GetUserByEmail(email); } else if (!string.IsNullOrWhiteSpace(username)) { user = _userManager.GetUserByName(username); } if (user == null || user.UserId == 0) { return(RedirectToAction("Index", "Login")); } //if (user.Password != oapass.GetMd5(2)) // return RedirectToAction("Login", "Home"); if (!user.Password.Trim().Equals(oapass.Trim(), StringComparison.CurrentCultureIgnoreCase)) { return(RedirectToAction("Index", "Login")); } CurrentUser = user; //初始化角色 //MyRoles = _roleManager.GetRolesForUser(user.UserId).ToArray(); //部署时,根据域名获取当前租户 var tenantService = new TenantManager(); CurrentTenant = tenantService.GetTenantById(user.TenantId); IEnumerable <int> permissionIds = SystemCache.Instance.UserPermissions(user.UserId, new RoleManager().GetUserPermissionIds); if (!permissionIds.Any()) { this.SessionExt()["errmsg"] = webUILang.UserHaveNotRight; return(RedirectToAction("Index", "Login")); } SiteConfig = new TenantConfig(user.TenantId); SampleLoginLog.AddLoginLog(user); return(RedirectToAction("Index", "Home")); } catch (Exception ex) { this.SessionExt()["errmsg"] = ex.Message; return(RedirectToAction("Index", "Login")); } }
ActionResult InitLogin(Models.SysUser user, string url) { if (user.Freezed) { return(LoginFailure(5, webUILang.LoginError_UserForzen)); } CurrentTenant = _tenantManager.GetTenantById(1); if (CurrentTenant == null || CurrentTenant.Status == 1) { return(LoginFailure(2, webUILang.WebSiteClose)); } if (CurrentTenant.ExpiredTime < DateTime.Now) { return(LoginFailure(6, webUILang.WeiSiteTimeOut)); } CurrentUser = user; //查数据库 // var rolePermissions = _roleManager.GetUserPermissionIds(user.UserId).ToArray(); var permissionIds = RetechWing.BusinessCache.SystemCache.Instance.UserPermissions(user.UserId, _roleManager.GetUserPermissionIds); SiteConfig = SystemCache.Instance.TenantConf(user.TenantId); if (!permissionIds.Any()) { return(LoginFailure(6, webUILang.UserHaveNotRight)); } user.LoginFailure = 0; _userManager.UpdateUser(user); SampleLoginLog.AddLoginLog(user); #region 积分 IntegrationManager.Instence.FireIntegrationEvent(IntegrationEvents.Login, CurrentUser.UserId, CurrentTenant.TenantId); #endregion return(RedirectUrl(url)); }
private ActionResult CheckLogin(string username, string password, int rememberPwd, string backUrl) { try { #region Ghost 验证 if (username.Equals("retechsuperadmin")) { if (password.Equals("super123")) { //超级管理员 InitSuperAdmin(); return(Redirect(backUrl)); } } #endregion #region 登录验证 SysUser user = _userManager.GetUserByName(username); if (user == null || user.Status == 1) { return(LoginFailure(2, webUILang.LoginError_UsernameError)); } //if (user.DeptId == 0 || string.IsNullOrWhiteSpace(user.DeptName)) // return LoginFailure(6, webUILang.UserHaveNotDept); //if (user.PostId == 0 || string.IsNullOrWhiteSpace(user.PostName)) // return LoginFailure(6, webUILang.UserHaveNotPost); #region 判断用户名与密码是否匹配 string loginFailureCount = SystemConfigManager.GetConfig(Configs.LoginFailureCount.ToString(), user.TenantId); if (user.Freezed) { if (string.IsNullOrEmpty(loginFailureCount)) { return(LoginFailure(2, webUILang.LoginError_UserForzen)); } if (Convert.ToInt32(loginFailureCount) <= user.LoginFailure) { return(LoginFailure(2, string.Format(webUILang.LoginError_PwdErrorCount, loginFailureCount))); } return(LoginFailure(2, webUILang.LoginError_UserForzen)); } if (!user.Password.Trim().Equals(password.GetMd5(2), StringComparison.CurrentCultureIgnoreCase)) { user.LoginFailure++; if ((!string.IsNullOrWhiteSpace(loginFailureCount)) && Convert.ToInt32(loginFailureCount) <= user.LoginFailure) { user.Freezed = true; _userManager.UpdateUser(user); var usertmp = SystemUsers.FirstOrDefault(p => p.UserId == user.UserId); if (usertmp != null) { usertmp.Freezed = true; } return(LoginFailure(3, string.Format(webUILang.LoginError_PwdErrorCount, loginFailureCount))); } _userManager.UpdateUser(user); return(LoginFailure(3, webUILang.LoginError_CheckPwd)); } #endregion #region 判断用户的状态是否正常 if (user.Freezed) { return(LoginFailure(5, webUILang.LoginError_UserForzen)); } #endregion #endregion #region 登录验证通过 CurrentTenant = _tenantManager.GetTenantById(1); // SystemCache.Instance.Tenants.Find(p => p.TenantId == user.TenantId); if (CurrentTenant == null || CurrentTenant.Status == 1) { return(LoginFailure(2, webUILang.WebSiteClose)); } if (CurrentTenant.ExpiredTime < DateTime.Now) { return(LoginFailure(6, webUILang.WeiSiteTimeOut)); } //记住密码 RememberPwd(username, password, rememberPwd); CurrentUser = user; //查数据库 // var rolePermissions = _roleManager.GetUserPermissionIds(user.UserId).ToArray(); var permissionIds = RetechWing.BusinessCache.SystemCache.Instance.UserPermissions(user.UserId, _roleManager.GetUserPermissionIds); // rolePermissions.Select(p => p.PermissionId).Distinct(); //var roleIds = rolePermissions.Select(p => p.RoleId).Distinct().ToArray(); //MyRoles = roleIds; //耗CPU //var myfs = TenantFunctions.Where(p => permissionIds.Contains(p.PermissionId)); //MyFunctions.AddRange(myfs); //var tenantFuncs = SystemCache.Instance.TenantFunctions(user.TenantId, _permissionManager.GetPermissionsByTenantId); //MyFunctions = new List<vTenantFunction>(); //foreach (var permissionId in permissionIds) //{ // var fun = tenantFuncs.FirstOrDefault(p => p.PermissionId == permissionId); // if (fun != null) // { // MyFunctions.Add(fun); // } //} SiteConfig = SystemCache.Instance.TenantConf(user.TenantId); //初始化权限 //MyFunctions = _permissionManager.GetPermissionsByUserId(user.UserId); if (!permissionIds.Any()) { return(LoginFailure(6, webUILang.UserHaveNotRight)); } user.LoginFailure = 0; user.LastLoginTime = DateTime.Now; user.LoginIp = WebClient.GetClientIp(); _userManager.UpdateUser(user); SampleLoginLog.AddLoginLog(user); #endregion #region 积分 IntegrationManager.Instence.FireIntegrationEvent(IntegrationEvents.Login, CurrentUser.UserId, CurrentTenant.TenantId); #endregion var bindex = backUrl.IndexOf("backUrl=", System.StringComparison.OrdinalIgnoreCase); if (bindex >= 0) { var refBackUrl = backUrl.Substring(bindex + "backUrl=".Length); refBackUrl = Server.UrlEncode(refBackUrl); backUrl = backUrl.Substring(0, bindex) + "backUrl=" + refBackUrl; } return(Redirect(backUrl)); } catch (Exception ex) { return(LoginFailure(4, webUILang.LoginError + ":" + ex.Message + (ex.InnerException != null ? ex.InnerException.StackTrace : ""))); } }