예제 #1
0
        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 sysUserBLL.CheckLogin(userName, password, (int)PlatformEnum.Web);

            if (userObj.Tag == 1)
            {
                try
                {
                    await new UserBLL().UpdateUser(userObj.Result);
                    await Operator.Instance.AddCurrent(userObj.Result.WebToken);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

            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.Result?.Id
                };

                // 让底层不用获取HttpContext
                logLoginEntity.BaseCreatorId = logLoginEntity.BaseCreatorId ?? 0;

                await new LogLoginBLL().SaveForm(logLoginEntity);
            };

            AsyncTaskHelper.StartTask(taskAction);

            obj.Tag     = userObj.Tag;
            obj.Message = userObj.Message;
            return(Json(obj));
        }
예제 #2
0
        public async Task <TData <string> > SaveForm(LogLoginEntity entity)
        {
            TData <string> obj = new TData <string>();
            await logLoginService.SaveForm(entity);

            obj.Data = entity.Id.ParseToString();
            obj.Tag  = 1;
            return(obj);
        }
예제 #3
0
        public async Task SaveForm(LogLoginEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                await entity.Create();

                await this.BaseRepository().Insert(entity);
            }
            else
            {
                await this.BaseRepository().Update(entity);
            }
        }
예제 #4
0
 public async Task SaveForm(LogLoginEntity entity)
 {
     if (entity.Id.IsNullOrZero())
     {
         // 默认赋值
         entity.Id = IdGeneratorHelper.Instance.GetId();
         ;
         await _logLoginEntityDB.InsertNowAsync(entity);
     }
     else
     {
         await _logLoginEntityDB.UpdateNowAsync(entity, ignoreNullValues : true);
     }
 }
예제 #5
0
        public async Task <IActionResult> LoginJson(string userName, string password, string captchaCode)
        {
            TData obj = new TData();
            TData <UserEntity> userObj = await userBLL.CheckLogin(userName, password, (int)PlatformEnum.Web);

            if (userObj.Tag == 1)
            {
                await new UserBLL().UpdateUser(userObj.Data);
                await Operator.Instance.AddCurrent(userObj.Data.WebToken);
            }

            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);

            obj.Tag     = userObj.Tag;
            obj.Message = userObj.Message;
            return(Json(obj));
        }
예제 #6
0
        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));
        }
예제 #7
0
        public async Task <TData <string> > SaveFormJson([FromForm] LogLoginEntity entity)
        {
            var obj = await _logLoginBLL.SaveForm(entity);

            return(obj);
        }
예제 #8
0
        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);
        }