/// <summary> /// 登录方法 /// </summary> /// <param name="loginName"></param> /// <param name="userPwd"></param> /// <returns></returns> public COUserIdentity Login(string loginName, string userPwd, CPEnum.DeviceTypeEnum device, ref string errorMsg) { COUser user = this.GetUserByLoginName(loginName, false, false); if (user == null) { errorMsg = "不存在登录名为[" + loginName + "]的用户,请重新输入!"; return(null); } if (user.UserPwd.Equals(this.UserPwdEncrypt(userPwd), StringComparison.CurrentCultureIgnoreCase) == false) { errorMsg = "登录名或密码不对,请重新输入!"; return(null); } Guid gId = Guid.NewGuid(); bool b = this.AddUserIdentity(user.Id, gId, device); if (b) { //记录登录日志 CPLogHelper.Instance().AddLog(user.Id, user.UserName, device, user.UserName + "登录成功!", "用户登录"); COUserIdentity userIden = this.GetUserIdentity(gId); this.AddUserSession(userIden, user); return(userIden); } else { errorMsg = "写入登录标识时出错!"; return(null); } }
public LoginReturn Login(string LoginName, string UserPwd, int DeviceType) { base.SetHeader(); LoginReturn re = new LoginReturn(); re.Result = true; try { string errorMsg = ""; COUserIdentity iden = COOrgans.Instance().Login(LoginName, UserPwd, CPEnum.ConvertDeviceTypeEnum(DeviceType), ref errorMsg); if (string.IsNullOrEmpty(errorMsg) == false) { re.Result = false; re.ErrorMsg = errorMsg; return(re); } re.UserId = iden.UserId; re.UserKey = iden.UserKey; return(re); } catch (Exception ex) { re.Result = false; re.ErrorMsg = ex.Message.ToString(); return(re); } }
private void AddUserSession(COUserIdentity userIden, COUser user) { CPAppContext.GetHttpContext().Session.SetString("UserId", userIden.UserId.ToString()); CPAppContext.GetHttpContext().Session.SetString("UserKey", userIden.UserKey.ToString()); CPAppContext.GetHttpContext().Session.SetString("UserName", user.UserName); CPAppContext.GetHttpContext().Session.SetString("UserLoginName", user.LoginName.ToString()); if (string.IsNullOrEmpty(user.UserPhotoPath)) { CPAppContext.GetHttpContext().Session.SetString("UserPhotoPath", ""); } else { CPAppContext.GetHttpContext().Session.SetString("UserPhotoPath", user.UserPhotoPath.ToString()); } //获取用户所在的角色 List <CORole> userRole = this.GetUserStaticRoles(user.Id); string RoleIds = ""; string RoleNames = ""; userRole.ForEach(t => { if (string.IsNullOrEmpty(RoleIds)) { RoleIds = t.Id.ToString(); RoleNames = t.RoleName; } else { RoleIds += "," + t.Id.ToString(); RoleNames += "," + t.RoleName; } }); CPAppContext.GetHttpContext().Session.SetString("RoleIds", RoleIds.ToString()); CPAppContext.GetHttpContext().Session.SetString("RoleNames", RoleNames.ToString()); //获取部门 List <CODep> depCol = this.GetDepByUser(user.Id); string DepIds = ""; string DepNames = ""; depCol.ForEach(t => { if (string.IsNullOrEmpty(DepIds)) { DepIds = t.Id.ToString(); DepNames = t.DepName; } else { DepIds += "," + t.Id.ToString(); DepNames += "," + t.DepName; } }); CPAppContext.GetHttpContext().Session.SetString("DepIds", DepIds.ToString()); CPAppContext.GetHttpContext().Session.SetString("DepNames", DepNames.ToString()); //获取当前用户拥有管理员权限的子系统 List <CPSystem> sysCol = CPSystemHelper.Instance().GetSystems(); string sysIds = ""; sysCol.ForEach(t => { if (string.IsNullOrEmpty(t.AdminUserIds)) { return; } if (t.AdminUserIds.Split(',').Contains(user.Id.ToString())) { if (string.IsNullOrEmpty(sysIds)) { sysIds = t.Id.ToString(); } else { sysIds += "," + t.Id.ToString(); } } }); CPAppContext.GetHttpContext().Session.SetString("UserAdminSysIds", sysIds.ToString()); }