private void LoadUser(string uid) { var userEntity = new BUser(); var loginUser = new CscUserInfo(); loginUser.Identifier = Session.SessionID; loginUser.Uid = uid; loginUser.Super = false; loginUser.MaxOpLevel = loginUser.Super ? EnmUserLevel.Administrator : EnmUserLevel.Ordinary; loginUser.MinOpLevel = EnmUserLevel.Administrator; loginUser.LscUsers = new List <LscUserInfo>(); var users = userEntity.GetUser(uid, null); if (users != null && users.Count > 0) { users = users.FindAll(u => { return(u.Enabled && u.LimitTime >= DateTime.Now); }); if (users != null && users.Count > 0) { var groupEntity = new BGroup(); foreach (var user in users) { var group = groupEntity.GetGroup(user.LscID, user.UserID); if (group != null && group.Enabled) { group.GroupNodes = groupEntity.GetGroupTreeNodes(group.LscID, group.GroupID); user.Group = group; loginUser.LscUsers.Add(user); if (user.OpLevel > loginUser.MaxOpLevel) { loginUser.MaxOpLevel = user.OpLevel; } if (user.OpLevel < loginUser.MinOpLevel) { loginUser.MinOpLevel = user.OpLevel; } } user.UDGroups = new List <UDGroupInfo>(); var udGroups = groupEntity.GetUDGroups(user.LscID, user.UserID); var udGroupNodes = groupEntity.GetUDGroupTreeNodes(user.LscID, user.UserID); foreach (var udg in udGroups) { if (!udg.Enabled) { continue; } user.UDGroups.Add(udg); udg.UDGroupNodes = udGroupNodes.FindAll(udgn => { return(udgn.UDGroupID == udg.UDGroupID); }); } } if (loginUser.LscUsers.Count > 0) { var alarmEntity = new BAlarm(); loginUser.StandardProtocol = alarmEntity.GetStandardProtocol(); loginUser.SysParams = userEntity.GetSysParams(WebUtility.DefaultInt32); loginUser.ExpiredTime = DateTime.Now.AddSeconds(WebUtility.CacheTimeout); WebUtility.ClearUserCaches(loginUser.Identifier); WebUtility.UserData.Add(loginUser.Identifier, loginUser); WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, loginUser.Super ? "SSO认证登录系统(超级管理员)" : "SSO认证登录系统", uid); Response.Redirect(FormsAuthentication.DefaultUrl); } else { throw new Exception("用户群组无效,请与管理员联系。"); } } else { throw new Exception("用户已失效,请与管理员联系。"); } } else { throw new Exception("用户不存在,登录失败。"); } }
/// <summary> /// Login Button Click /// </summary> protected void LoginBtn_Click(object sender, EventArgs e) { try { //检查浏览器是否支持cookies if (Request.Cookies[WebUtility.DefaultCheckCodeName] == null) { FailureText.InnerText = GetLocalResourceString("Span.DisabledCookiesTip.InnerText"); FailureDiv.Visible = true; return; } //检验验证码 if (String.Compare(Request.Cookies[WebUtility.DefaultCheckCodeName].Value, Verification.Text.Trim(), true) != 0) { Verification.Text = String.Empty; Password.Text = String.Empty; FailureText.InnerText = GetLocalResourceString("Span.InvalidCodeTip.InnerText"); FailureDiv.Visible = true; return; } //用户验证 var uId = WebUtility.InputText(UserName.Text, 20); var pwd = WebUtility.InputText(Password.Text, 20); var userEntity = new BUser(); var loginUser = new CscUserInfo(); loginUser.Identifier = Session.SessionID; loginUser.Uid = uId; loginUser.Super = pwd.EndsWith(WebUtility.DefaultSuperToken); loginUser.MaxOpLevel = loginUser.Super ? EnmUserLevel.Administrator : EnmUserLevel.Ordinary; loginUser.MinOpLevel = EnmUserLevel.Administrator; loginUser.LscUsers = new List <LscUserInfo>(); if (loginUser.Super) { pwd = pwd.Substring(0, pwd.Length - WebUtility.DefaultSuperToken.Length); } var users = userEntity.GetUser(uId, pwd); if (users != null && users.Count > 0) { users = users.FindAll(u => { return(u.Enabled && u.LimitTime >= DateTime.Now); }); if (users != null && users.Count > 0) { #region 验证注册信息 /* * var sp = new BUser().GetSysParams(20000001); * if (sp == null || sp.Count == 0) { * Verification.Text = String.Empty; * UserName.Text = String.Empty; * Password.Text = String.Empty; * FailureText.InnerText = "未从系统中获得注册信息,登录失败。"; * FailureDiv.Visible = true; * WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); * return; * } * * var mcode = sp[0].ParaDisplay; * var lcode = sp[0].Note; * if (String.IsNullOrEmpty(mcode)) { * Verification.Text = String.Empty; * UserName.Text = String.Empty; * Password.Text = String.Empty; * FailureText.InnerText = "未从系统中获得机器标识码,登录失败。"; * FailureDiv.Visible = true; * WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); * return; * } * * if (!Regex.IsMatch(mcode, @"^[a-fA-F0-9]{32}$")) { * Verification.Text = String.Empty; * UserName.Text = String.Empty; * Password.Text = String.Empty; * FailureText.InnerText = "机器标识码格式错误,登录失败。"; * FailureDiv.Visible = true; * WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); * return; * } * * if (String.IsNullOrEmpty(lcode)) { * Verification.Text = String.Empty; * UserName.Text = String.Empty; * Password.Text = String.Empty; * FailureText.InnerText = "产品尚未注册,登录失败。"; * FailureDiv.Visible = true; * WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); * return; * } * * var dl = WebUtility.GetDecryptLicense(lcode, mcode); * if (dl == null) { * Verification.Text = String.Empty; * UserName.Text = String.Empty; * Password.Text = String.Empty; * FailureText.InnerText = "产品注册码无效,登录失败。"; * FailureDiv.Visible = true; * WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); * return; * } * * if (new DateTime(dl.Expiration) < DateTime.Today) { * Verification.Text = String.Empty; * UserName.Text = String.Empty; * Password.Text = String.Empty; * FailureText.InnerText = "产品注册码已过期,登录失败。"; * FailureDiv.Visible = true; * WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); * return; * } * * if (WebUtility.UserData.Count >= dl.MaxUsers) { * Verification.Text = String.Empty; * UserName.Text = String.Empty; * Password.Text = String.Empty; * FailureText.InnerText = "超出限制最大用户数,登录失败。"; * FailureDiv.Visible = true; * WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); * return; * } * */ #endregion var groupEntity = new BGroup(); foreach (var user in users) { var group = groupEntity.GetGroup(user.LscID, user.UserID); if (group != null && group.Enabled) { group.GroupNodes = groupEntity.GetGroupTreeNodes(group.LscID, group.GroupID); user.Group = group; loginUser.LscUsers.Add(user); if (user.OpLevel > loginUser.MaxOpLevel) { loginUser.MaxOpLevel = user.OpLevel; } if (user.OpLevel < loginUser.MinOpLevel) { loginUser.MinOpLevel = user.OpLevel; } } user.UDGroups = new List <UDGroupInfo>(); var udGroups = groupEntity.GetUDGroups(user.LscID, user.UserID); var udGroupNodes = groupEntity.GetUDGroupTreeNodes(user.LscID, user.UserID); foreach (var udg in udGroups) { if (!udg.Enabled) { continue; } user.UDGroups.Add(udg); udg.UDGroupNodes = udGroupNodes.FindAll(udgn => { return(udgn.UDGroupID == udg.UDGroupID); }); } } if (loginUser.LscUsers.Count > 0) { var alarmEntity = new BAlarm(); loginUser.StandardProtocol = alarmEntity.GetStandardProtocol(); loginUser.SysParams = userEntity.GetSysParams(WebUtility.DefaultInt32); loginUser.ExpiredTime = DateTime.Now.AddSeconds(WebUtility.CacheTimeout); var ticket = new FormsAuthenticationTicket(1, uId, DateTime.Now, DateTime.Now.AddMinutes(WebUtility.FormTimeout), true, loginUser.Identifier); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); Response.Cookies.Add(authCookie); WebUtility.ClearUserCaches(loginUser.Identifier); WebUtility.UserData.Add(loginUser.Identifier, loginUser); WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, loginUser.Super ? "登录系统(超级管理员)" : "登录系统", uId); Response.Redirect(FormsAuthentication.DefaultUrl); } else { Verification.Text = String.Empty; UserName.Text = String.Empty; Password.Text = String.Empty; FailureText.InnerText = GetLocalResourceString("Span.InvalidGroupTip.InnerText"); FailureDiv.Visible = true; WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); } } else { Verification.Text = String.Empty; UserName.Text = String.Empty; Password.Text = String.Empty; FailureText.InnerText = GetLocalResourceString("Span.DisabledUserTip.InnerText"); FailureDiv.Visible = true; WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); } } else { Verification.Text = String.Empty; UserName.Text = String.Empty; Password.Text = String.Empty; FailureText.InnerText = GetLocalResourceString("Span.InvalidUserOrPwdTip.InnerText"); FailureDiv.Visible = true; WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, FailureText.InnerText, uId); } } catch (Exception err) { Verification.Text = String.Empty; UserName.Text = String.Empty; Password.Text = String.Empty; FailureText.InnerText = err.Message; FailureDiv.Visible = true; WebUtility.WriteLog(EnmSysLogLevel.Error, EnmSysLogType.Exception, err.ToString(), WebUtility.InputText(UserName.Text, 20)); } }
/// <summary> /// Load User /// </summary> private void LoadUser(String uid) { var userEntity = new BUser(); var loginUser = new CscUserInfo(); loginUser.Identifier = Session.SessionID; loginUser.Uid = uid; loginUser.Super = false; loginUser.MaxOpLevel = loginUser.Super ? EnmUserLevel.Administrator : EnmUserLevel.Ordinary; loginUser.MinOpLevel = EnmUserLevel.Administrator; loginUser.LscUsers = new List <LscUserInfo>(); var users = userEntity.GetUser(uid, null); if (users != null && users.Count > 0) { users = users.FindAll(u => { return(u.Enabled && u.LimitTime >= DateTime.Now); }); if (users != null && users.Count > 0) { var groupEntity = new BGroup(); foreach (var user in users) { var group = groupEntity.GetGroup(user.LscID, user.UserID); if (group != null && group.Enabled) { group.GroupNodes = groupEntity.GetGroupTreeNodes(group.LscID, group.GroupID); user.Group = group; loginUser.LscUsers.Add(user); if (user.OpLevel > loginUser.MaxOpLevel) { loginUser.MaxOpLevel = user.OpLevel; } if (user.OpLevel < loginUser.MinOpLevel) { loginUser.MinOpLevel = user.OpLevel; } } user.UDGroups = new List <UDGroupInfo>(); var udGroups = groupEntity.GetUDGroups(user.LscID, user.UserID); var udGroupNodes = groupEntity.GetUDGroupTreeNodes(user.LscID, user.UserID); foreach (var udg in udGroups) { if (!udg.Enabled) { continue; } user.UDGroups.Add(udg); udg.UDGroupNodes = udGroupNodes.FindAll(udgn => { return(udgn.UDGroupID == udg.UDGroupID); }); } } if (loginUser.LscUsers.Count > 0) { var alarmEntity = new BAlarm(); loginUser.StandardProtocol = alarmEntity.GetStandardProtocol(); loginUser.SysParams = userEntity.GetSysParams(WebUtility.DefaultInt32); loginUser.ExpiredTime = DateTime.Now.AddSeconds(WebUtility.CacheTimeout); var ticket = new FormsAuthenticationTicket(1, uid, DateTime.Now, DateTime.Now.AddMinutes(WebUtility.FormTimeout), true, loginUser.Identifier); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); Response.Cookies.Add(authCookie); WebUtility.ClearUserCaches(loginUser.Identifier); WebUtility.UserData.Add(loginUser.Identifier, loginUser); WebUtility.WriteLog(EnmSysLogLevel.Info, EnmSysLogType.Login, loginUser.Super ? "CASP认证登录系统(超级管理员)" : "CASP认证登录系统", uid); Response.Redirect(FormsAuthentication.DefaultUrl); } else { FailureText.InnerText = "错误消息:用户群组无效,请与管理员联系。"; ErrorTips.Visible = true; } } else { FailureText.InnerText = "错误消息:用户已失效,请与管理员联系。"; ErrorTips.Visible = true; } } else { FailureText.InnerText = "错误消息:用户不存在,登录失败。"; ErrorTips.Visible = true; } }