public async Task <IActionResult> LoginJson(string userName, string password, string captchaCode) { TData obj = new TData(); if (string.IsNullOrEmpty(captchaCode)) { obj.Message = "验证码不能为空"; return(Json(obj)); } if (captchaCode != new SessionHelper().GetSession("CaptchaCode").ParseToString()) { obj.Message = "验证码错误,请重新输入"; return(Json(obj)); } // TData<UserEntity> userObj = await userBLL.CheckLogin(userName, password, (int)PlatformEnum.Web); TData <UserEntity> userObj = await userBLL.CheckLogin(userName, password); if (userObj.Tag == 1) { // await new UserBLL().UpdateUser(userObj.Data); // await Operator.Instance.AddCurrent(userObj.Data.WebToken); } await _operator.AddCurrent(userObj.Data.ApiToken); string ip = NetHelper.Ip; string browser = NetHelper.Browser; string os = NetHelper.GetOSVersion(); string userAgent = NetHelper.UserAgent; Action taskAction = async() => { LogLoginEntity logLoginEntity = new LogLoginEntity { LogStatus = userObj.Tag == 1 ? OperateStatusEnum.Success.ParseToInt() : OperateStatusEnum.Fail.ParseToInt(), Remark = userObj.Message, IpAddress = ip, IpLocation = IpLocationHelper.GetIpLocation(ip), Browser = browser, OS = os, ExtraRemark = userAgent, // BaseCreatorId = userObj.Data?.Id }; // 让底层不用获取HttpContext // logLoginEntity.BaseCreatorId = logLoginEntity.BaseCreatorId ?? 0; await logLoginBLL.SaveForm(logLoginEntity); }; // AsyncTaskHelper.StartTask(taskAction); var claims = new List <Claim> { new Claim(ClaimTypes.Name, userObj.Data.UserName), new Claim("ApiToken", userObj.Data.ApiToken), }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties(); await NetHelper.HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); obj.Tag = userObj.Tag; obj.Message = userObj.Message; return(Json(obj)); }
public async Task <TData <OperatorInfo> > Login([FromForm] string userName, [FromForm] string password) { var obj = new TData <OperatorInfo>(); var userObj = await _userBLL.CheckLogin(userName, password); if (userObj.Tag == 1) { await _userBLL.UpdateLoginInfo(userObj.Data); await _operatorCache.AddCurrent(userObj.Data.ApiToken); obj.Data = await _operatorCache.Current(userObj.Data.ApiToken); } obj.Message = userObj.Message; var ip = NetHelper.Ip; var browser = NetHelper.Browser; var os = NetHelper.GetOSVersion(); var userAgent = NetHelper.UserAgent; var logLoginEntity = new LogLoginEntity { LogStatus = userObj.Tag == 1 ? OperateStatusEnum.Success.ParseToInt() : OperateStatusEnum.Fail.ParseToInt(), Remark = userObj.Message, IpAddress = ip, IpLocation = IpLocationHelper.GetIpLocation(ip), Browser = browser, OS = os, ExtraRemark = userAgent, CreatorId = userObj.Data == null ? 0 : userObj.Data.Id, CreateTime = DateTime.Now }; await _logLoginBLL.SaveForm(logLoginEntity); if (userObj.Tag == 0) { return(obj); } // 生成前端的token // 生成 token var jwtSettings = App.GetOptions <JWTSettingsOptions>(); var datetimeOffset = DateTimeOffset.UtcNow; var accessToken = JWTEncryption.Encrypt(jwtSettings.IssuerSigningKey, new Dictionary <string, object> { { "UserId", userObj.Data.Id.ToString() }, // 存储Id { "Account", userObj.Data.UserName }, // 存储用户名 { "ApiToken", userObj.Data.ApiToken }, // ApiToken { JwtRegisteredClaimNames.Iat, datetimeOffset.ToUnixTimeSeconds() }, { JwtRegisteredClaimNames.Nbf, datetimeOffset.ToUnixTimeSeconds() }, { JwtRegisteredClaimNames.Exp, DateTimeOffset.UtcNow.AddSeconds(jwtSettings.ExpiredTime.Value * 60).ToUnixTimeSeconds() }, { JwtRegisteredClaimNames.Iss, jwtSettings.ValidIssuer }, { JwtRegisteredClaimNames.Aud, jwtSettings.ValidAudience } }); // 覆盖apitoken,因为前端需要的是jwt生成的token,而缓存使用的是数据库的apitoken字段 obj.Data.JwtToken = accessToken; obj.Tag = userObj.Tag; var claims = new List <Claim> { new Claim(ClaimTypes.Name, userObj.Data.UserName), new Claim("ApiToken", userObj.Data.ApiToken), }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties(); await NetHelper.HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); return(obj); }