public async Task <IActionResult> GetCheckUser(string username, string password, string vcode, string vkey, string appId, string systemCode) { CommonResult result = new CommonResult(); RemoteIpParser remoteIpParser = new RemoteIpParser(); string strIp = remoteIpParser.GetClientIp(HttpContext).MapToIPv4().ToString(); YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper(); var vCode = yuebonCacheHelper.Get("ValidateCode" + vkey); string code = vCode != null?vCode.ToString() : "11"; if (vcode.ToUpper() != code) { result.ErrMsg = "验证码错误"; return(ToJsonContent(result)); } Log logEntity = new Log(); bool blIp = _filterIPService.ValidateIP(strIp); if (blIp) { result.ErrMsg = strIp + "该IP已被管理员禁止登录!"; } else { if (string.IsNullOrEmpty(username)) { result.ErrMsg = "用户名不能为空!"; } else if (string.IsNullOrEmpty(password)) { result.ErrMsg = "密码不能为空!"; } if (string.IsNullOrEmpty(systemCode)) { result.ErrMsg = ErrCode.err40006; } else { string strHost = Request.Host.ToString(); APP app = _appService.GetAPP(appId); if (app == null) { result.ErrCode = "40001"; result.ErrMsg = ErrCode.err40001; } else { if (!app.RequestUrl.Contains(strHost, StringComparison.Ordinal) && !strHost.Contains("localhost", StringComparison.Ordinal)) { result.ErrCode = "40002"; result.ErrMsg = ErrCode.err40002 + ",你当前请求主机:" + strHost; } else { SystemType systemType = _systemTypeService.GetByCode(systemCode); if (systemType == null) { result.ErrMsg = ErrCode.err40006; } else { Tuple <User, string> userLogin = await this._userService.Validate(username, password); if (userLogin != null) { string ipAddressName = IpAddressUtil.GetCityByIp(strIp); if (userLogin.Item1 != null) { result.Success = true; User user = userLogin.Item1; JwtOption jwtModel = App.GetService <JwtOption>(); TokenProvider tokenProvider = new TokenProvider(jwtModel); TokenResult tokenResult = tokenProvider.LoginToken(user, appId); YuebonCurrentUser currentSession = new YuebonCurrentUser { UserId = user.Id, Name = user.RealName, AccessToken = tokenResult.AccessToken, AppKey = appId, CreateTime = DateTime.Now, Role = _roleService.GetRoleEnCode(user.RoleId), ActiveSystemId = systemType.Id, CurrentLoginIP = strIp, IPAddressName = ipAddressName }; TimeSpan expiresSliding = DateTime.Now.AddMinutes(120) - DateTime.Now; yuebonCacheHelper.Add("login_user_" + user.Id, currentSession, expiresSliding, true); List <AllowCacheApp> list = yuebonCacheHelper.Get("AllowAppId").ToJson().ToList <AllowCacheApp>(); if (list.Count == 0) { IEnumerable <APP> appList = _appService.GetAllByIsNotDeleteAndEnabledMark(); yuebonCacheHelper.Add("AllowAppId", appList); } CurrentUser = currentSession; result.ResData = currentSession; result.ErrCode = ErrCode.successCode; result.Success = true; logEntity.Account = user.Account; logEntity.NickName = user.NickName; logEntity.Date = logEntity.CreatorTime = DateTime.Now; logEntity.IPAddress = CurrentUser.CurrentLoginIP; logEntity.IPAddressName = CurrentUser.IPAddressName; logEntity.Result = true; logEntity.ModuleName = "登录"; logEntity.Description = "登录成功"; logEntity.Type = "Login"; _logService.Insert(logEntity); } else { result.ErrCode = ErrCode.failCode; result.ErrMsg = userLogin.Item2; logEntity.Account = username; logEntity.Date = logEntity.CreatorTime = DateTime.Now; logEntity.IPAddress = strIp; logEntity.IPAddressName = ipAddressName; logEntity.Result = false; logEntity.ModuleName = "登录"; logEntity.Type = "Login"; logEntity.Description = "登录失败," + userLogin.Item2; _logService.Insert(logEntity); } } } } } } } yuebonCacheHelper.Remove("LoginValidateCode"); return(ToJsonContent(result, true)); }
public IActionResult SysConnect(string openmf, string appId, string systemCode) { CommonResult result = new CommonResult(); RemoteIpParser remoteIpParser = new RemoteIpParser(); string strIp = remoteIpParser.GetClientIp(HttpContext).MapToIPv4().ToString(); if (string.IsNullOrEmpty(openmf)) { result.ErrMsg = "切换参数错误!"; } bool blIp = _filterIPService.ValidateIP(strIp); if (blIp) { result.ErrMsg = strIp + "该IP已被管理员禁止登录!"; } else { string ipAddressName = IpAddressUtil.GetCityByIp(strIp); if (string.IsNullOrEmpty(systemCode)) { result.ErrMsg = ErrCode.err40006; } else { string strHost = Request.Host.ToString(); APP app = _appService.GetAPP(appId); if (app == null) { result.ErrCode = "40001"; result.ErrMsg = ErrCode.err40001; } else { if (!app.RequestUrl.Contains(strHost, StringComparison.Ordinal) && !strHost.Contains("localhost", StringComparison.Ordinal)) { result.ErrCode = "40002"; result.ErrMsg = ErrCode.err40002 + ",你当前请求主机:" + strHost; } else { SystemType systemType = _systemTypeService.GetByCode(systemCode); if (systemType == null) { result.ErrMsg = ErrCode.err40006; } else { YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper(); object cacheOpenmf = yuebonCacheHelper.Get("openmf" + openmf); yuebonCacheHelper.Remove("openmf" + openmf); if (cacheOpenmf == null) { result.ErrCode = "40007"; result.ErrMsg = ErrCode.err40007; } else { User user = _userService.Get(cacheOpenmf.ToString()); if (user != null) { result.Success = true; JwtOption jwtModel = App.GetService <JwtOption>(); TokenProvider tokenProvider = new TokenProvider(jwtModel); TokenResult tokenResult = tokenProvider.LoginToken(user, appId); YuebonCurrentUser currentSession = new YuebonCurrentUser { UserId = user.Id, Name = user.RealName, AccessToken = tokenResult.AccessToken, AppKey = appId, CreateTime = DateTime.Now, Role = _roleService.GetRoleEnCode(user.RoleId), ActiveSystemId = systemType.Id, CurrentLoginIP = strIp, IPAddressName = ipAddressName, ActiveSystemUrl = systemType.Url }; TimeSpan expiresSliding = DateTime.Now.AddMinutes(120) - DateTime.Now; yuebonCacheHelper.Add("login_user_" + user.Id, currentSession, expiresSliding, true); CurrentUser = currentSession; result.ResData = currentSession; result.ErrCode = ErrCode.successCode; result.Success = true; } else { result.ErrCode = ErrCode.failCode; } } } } } } } return(ToJsonContent(result)); }