Esempio n. 1
0
        public async Task <IActionResult> Login([FromBody] AuthLoginInput loginModel)
        {
            var sw = new Stopwatch();

            sw.Start();
            var res = await _authService.LoginAsync(loginModel);

            //  var  res = await _authApi.Login(loginModel);
            sw.Stop();
            #region

            var loginLogAddInput = new LoginLogAddInput()
            {
                CreatedUserName     = loginModel.UserName,
                ElapsedMilliseconds = sw.ElapsedMilliseconds,
                Status = res.Success,
                Msg    = res.Msg
            };

            ResultModel <AuthLoginOutput> output = null;
            if (res.Success)
            {
                output = res as ResultModel <AuthLoginOutput>;
                var _user = output.Data;
                loginLogAddInput.CreatedUserId = _user.Id;
                loginLogAddInput.NickName      = _user.NickName;
            }

            // await _loginLogService.AddAsync(loginLogAddInput);
            #endregion

            if (!res.Success)
            {
                return(Json(res));
            }
            var userData = output.Data;
            var claims   = new[]
            {
                new Claim(ClaimAttributes.UserId, userData.Id.ToString()),
                new Claim(ClaimAttributes.UserName, userData.UserName),
                new Claim(ClaimAttributes.UserNickName, userData.NickName)
            };
            var             claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
            ClaimsPrincipal user           = new ClaimsPrincipal(claimsIdentity);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user);

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                user, new AuthenticationProperties()
            {
                IsPersistent = true,
                ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(60),
                AllowRefresh = true
            });

            return(Json(res));
        }
Esempio n. 2
0
        public async Task <IResponseOutput> Login(AuthLoginInput input)
        {
            var sw = new Stopwatch();

            sw.Start();
            var res = (await _authService.LoginAsync(input)) as IResponseOutput;

            sw.Stop();

            #region 添加登录日志
            var loginLogAddInput = new LoginLogAddInput()
            {
                CreatedUserName     = input.UserName,
                ElapsedMilliseconds = sw.ElapsedMilliseconds,
                Status = res.Success,
                Msg    = res.Msg
            };

            AuthLoginOutput user = null;
            if (res.Success)
            {
                user = (res as IResponseOutput <AuthLoginOutput>).Data;
                loginLogAddInput.CreatedUserId = user.Id;
                loginLogAddInput.RealName      = user.Name;
            }

            await _loginLogService.AddAsync(loginLogAddInput);

            #endregion

            if (!res.Success)
            {
                return(res);
            }

            #region 生成token信息
            var token = _userToken.Build(new[]
            {
                new Claim(ClaimAttributes.UserId, user.Id.ToString()),
                new Claim(ClaimAttributes.UserName, user.UserName),
                new Claim(ClaimAttributes.UserRealName, user.Name)
            });
            #endregion

            return(ResponseOutput.Ok(new { token }));
        }
Esempio n. 3
0
        public async Task <IResponseOutput <long> > AddAsync(LoginLogAddInput input)
        {
            var res = new ResponseOutput <long>();

            input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);

            string ua     = _context.HttpContext.Request.Headers["User-Agent"];
            var    client = UAParser.Parser.GetDefault().Parse(ua);
            var    device = client.Device.Family;

            device            = device.ToLower() == "other" ? "" : device;
            input.Browser     = client.UA.Family;
            input.Os          = client.OS.Family;
            input.Device      = device;
            input.BrowserInfo = ua;

            var entity = _mapper.Map <LoginLogEntity>(input);
            var id     = (await _loginLogRepository.InsertAsync(entity)).Id;

            return(id > 0 ? res.Ok(id) : res);
        }
Esempio n. 4
0
        public async Task <IResponseOutput> AddAsync(LoginLogAddInput input)
        {
            input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);

            string ua = _context.HttpContext.Request.Headers["User-Agent"];

            if (ua.NotNull())
            {
                var client = UAParser.Parser.GetDefault().Parse(ua);
                var device = client.Device.Family;
                device            = device.ToLower() == "other" ? "" : device;
                input.Browser     = client.UA.Family;
                input.Os          = client.OS.Family;
                input.Device      = device;
                input.BrowserInfo = ua;
            }
            var entity = ObjectMapper.Map <LoginLogEntity>(input);
            await _loginLogRepository.InsertAsync(entity);

            return(ResponseOutput.Ok());
        }
Esempio n. 5
0
        public async Task <IResponseOutput> Login(AuthLoginInput input)
        {
            var sw = new Stopwatch();

            sw.Start();
            var res = await _authService.LoginAsync(input);

            sw.Stop();

            #region 添加登录日志

            var loginLogAddInput = new LoginLogAddInput()
            {
                CreatedUserName     = input.UserName,
                ElapsedMilliseconds = sw.ElapsedMilliseconds,
                Status = res.Success,
                Msg    = res.Msg
            };

            ResponseOutput <AuthLoginOutput> output = null;
            if (res.Success)
            {
                output = (res as ResponseOutput <AuthLoginOutput>);
                var user = output.Data;
                loginLogAddInput.CreatedUserId = user.Id;
                loginLogAddInput.NickName      = user.NickName;
                loginLogAddInput.TenantId      = user.TenantId;
            }

            await _loginLogService.AddAsync(loginLogAddInput);

            #endregion 添加登录日志

            if (!res.Success)
            {
                return(res);
            }

            return(GetToken(output));
        }
Esempio n. 6
0
        public async Task <IResultModel> AddAsync(LoginLogAddInput input)
        {
            input.IP = IPHelper.GetIP(_context?.HttpContext?.Request);
            string ua = _context.HttpContext.Request.Headers["User-Agent"];

            if (ua.NotNull())
            {
                var client = UAParser.Parser.GetDefault().Parse(ua);
                var device = client.Device.Family;
                device            = device.ToLower() == "other" ? "" : device;
                input.Browser     = client.UA.Family;
                input.Os          = client.OS.Family;
                input.Device      = device;
                input.BrowserInfo = ua;
            }
            var entity = _mapper.Map <LoginLogEntity>(input);

            entity.CreatedTime = DateTime.UtcNow;
            var id = (await _loginLogRepository.InsertAsync(entity));

            return(ResultModel.Result(id > 0));
        }