Esempio n. 1
0
        public async Task <IResultModel> Login([FromBody] LoginModel model)
        {
            var log = new LoginLogAddModel();

            try
            {
                var result = await _authService.Login(model, _webConfigModel.RefreshTokenExpiredTime);

                log.Result      = result.Successful;
                log.IP          = _loginInfo.IP;
                log.BrowserInfo = _accessor.HttpContext.Request.Headers["User-Agent"];
                log.UserCode    = model.UserCode;
                log.Remark      = result.Msg;

                if (result.Successful)
                {
                    var user      = result.Data.User;
                    var loginInfo = result.Data.AuthInfo;

                    log.UserName = user.UserName;

                    var claims = new[]
                    {
                        new Claim(ClaimsName.UserId, user.Id.ToString()),
                        new Claim(ClaimsName.UserCode, user.UserCode),
                        new Claim(ClaimsName.UserName, user.UserName),
                        new Claim(ClaimsName.OrganizeId, user.OrganizeId.ToString()),
                        new Claim(ClaimsName.LoginTime, loginInfo.LoginTime.ToString()),
                        new Claim(ClaimsName.RoleIds, result.Data.RoleIds),
                        new Claim(ClaimsName.RoleCodes, result.Data.RoleCodes),
                        new Claim(ClaimsName.RoleNames, result.Data.RoleNames)
                    };


                    var token = _jwtHandler.Build(claims, loginInfo.RefreshToken);
                    return(ResultModel.Success(token));
                }

                return(ResultModel.Failed(result.Msg));
            }
            catch (Exception ex)
            {
                log.Remark = ex.Message;
                return(ResultModel.Failed(ex.Message));
            }
            finally
            {
                await _loginLogService.Add(log);
            }
        }