public void recordLogininfor(string userName, char status, string message, RequestBasicInfo info) { SysLoginInfor model = new SysLoginInfor(); model.UserName = userName; model.status = status; model.msg = message; var task = IPAddressHelper.getRealAddressByIP(info.Ip); model.ipaddr = info.Ip; model.LoginLocation = task.Result; model.Browser = info.Device; model.Os = info.Os; model.LoginTime = DateTime.Now; //这里为社么不用这个方法 原因: //因为这个方法是在一个Task 任务里启动的,这样子会造成 注入的仓储相关的dbcontent 其实已经被dispose了。 //如果不用多线程,this.Add方法是完全可以用的; //this.Add(model); using (var context = new YouGeDbContext(option)) { context.Set <SysLoginInfor>().Add(model); context.SaveChanges(); } }
public void setUserAgent(LoginUser loginUser, RequestBasicInfo info) { loginUser.ipaddr = info.Ip; loginUser.loginLocation = "TO DO "; loginUser.browser = info.Device; loginUser.os = info.Os; }
/// <summary> /// 获取远程访问用户的Ip地址 /// </summary> /// <returns>返回Ip地址</returns> protected RequestBasicInfo GetRequestInfo(IHttpContextAccessor httpContextAccessor) { RequestBasicInfo info = new RequestBasicInfo(); var ip = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault(); if (string.IsNullOrEmpty(ip)) { ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); if (ip == "0.0.0.1") { ip = httpContextAccessor.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString(); } } info.Ip = ip; info.RequestTime = DateTime.Now; info.RequestType = httpContextAccessor.HttpContext.Request.Method; info.RequestUrl = httpContextAccessor.HttpContext.Request.GetDisplayUrl(); string UserAgent = httpContextAccessor.HttpContext.Request.Headers["User-Agent"]; var RequestInfo = new ReqUAInfoCollector(UserAgent).Parse(); info.Device = RequestInfo.BrowserName + RequestInfo.BrowserVersion; info.Os = RequestInfo.SystemName + RequestInfo.SystemVersion; return(info); }
public AjaxReponseBase Login([FromBody] LoginModel model) { AjaxReponseBase response = AjaxReponseBase.Success(); // 生成令牌 RequestBasicInfo info = this.GetRequestInfo(httpContextAccessor); string token = loginService.login(model.username, model.password, model.code, model.uuid, info); response.Add(YouGeSystemConst.TOKEN, token); return(response); }
public string createToken(LoginUser loginUser, RequestBasicInfo info) { // string token = IdUtils.fastUUID(); // TO DO string token = Guid.NewGuid().ToString().Replace("-", ""); loginUser.token = token; setUserAgent(loginUser, info); refreshToken(loginUser); var claims = new Claim[] { new Claim(YouGeSystemConst.LOGIN_USER_KEY, token) }; return(createToken(claims)); }
public string login(string username, string password, string code, string uuid, RequestBasicInfo info) { string verifyKey = YouGeSystemConst.CAPTCHA_CODE_KEY + uuid; string captcha = YouGeRedisHelper.Get(verifyKey); YouGeRedisHelper.Del(verifyKey); if (captcha == null) { //启动线程 记录日志 var ta = new Task(() => sysLoginRepository.recordLogininfor(username, YouGeSystemConst.FAIL, "没有验证码", info) ); ta.Start(); throw new CaptchaExpireException(); } if (!string.Equals(code, captcha, StringComparison.OrdinalIgnoreCase)) { var tb = new Task(() => sysLoginRepository.recordLogininfor(username, YouGeSystemConst.FAIL, "验证码已失效", info) ); tb.Start(); throw new CaptchaException(); } try { LoginUser loginUser = this.loadUserByUsername(username, password); var tf = new Task(() => sysLoginRepository.recordLogininfor(username, YouGeSystemConst.SUCCESS, "登录成功", info) ); tf.Start(); // 生成token return(tokenService.createToken(loginUser, info)); } catch (Exception e) { if (e.Message.Contains("密码错误")) { var tc = new Task(() => sysLoginRepository.recordLogininfor(username, YouGeSystemConst.FAIL, "用户不存在/密码错误", info) ); tc.Start(); throw new UserPasswordNotMatchException(); } else { var td = new Task(() => sysLoginRepository.recordLogininfor(username, YouGeSystemConst.FAIL, e.Message, info) ); td.Start(); throw new CustomException(e.Message); } } }