Example #1
0
        public async Task <ApiResult <LoginResult> > UserLogin([FromBody] UserLoginInfo loginInfo)
        {
            var result = new ApiResult <LoginResult>().Success();

            _logger.Info("test");

            if (loginInfo == null)
            {
                return(result.Error("无效的登录信息"));
            }

            try
            {
                var user = await _repository.FirstOrDefaultAsync(x => x.UserName == loginInfo.LoginName && x.WebSiteId == loginInfo.WebSiteId);

                if (user == null)
                {
                    return(result.Error("账号不存在"));
                }

                if (user.Password != Encryptor.MD5Entry(loginInfo.Password))
                {
                    return(result.Error("账号或密码错误"));
                }

                var claims = new List <Claim>
                {
                    new Claim(ClaimConst.WebSiteId, user.WebSiteId),
                    new Claim(ClaimConst.UserId, user.Id),
                    new Claim(ClaimConst.UserName, user.UserName),
                    new Claim(JwtRegisteredClaimNames.Sub, "CXY.CJS"),
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N"))
                };

                var token = _jwtTokenProvider.GenerateJwtToken(claims);

                result.Data = new LoginResult {
                    Token = token, Expired = _jwtBearerConfig.ValidMinutes * 60
                };
            }
            catch (EntityNotFoundException)
            {
                result.Code    = 0;
                result.Message = "账号或密码错误";
            }
            catch (Exception ex)
            {
                result.Error(ex.Message);
            }

            return(result);
        }
Example #2
0
        public string GetToken(int id)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimConst.WebSiteId, "009020"),
                new Claim(ClaimConst.UserId, "009020123456"),
                new Claim(ClaimConst.UserName, "chelutong"),
                new Claim(JwtRegisteredClaimNames.Sub, "hausthy"),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
                new Claim(ClaimTypes.Role, "Admin")
            };

            return(_jwtTokenProvider.GenerateJwtToken(claims));
        }
Example #3
0
        public async Task <IActionResult> Login()
        {
            var BaseUrl       = _configuration["UiHomeAfterLogin"];
            var cookieContent = HttpContext.Request.Cookies["User"].ToString();

            if (string.IsNullOrEmpty(cookieContent))
            {
                return(BadRequest("UserNotLogged"));
            }

            var value = JsonConvert.DeserializeObject <UserData>(cookieContent);

            var user = await _dataService.UserService.ExternalGetAsync(value.ssoID);

            if (user?.Id == null)
            {
                return(BadRequest("UserNotRegistered"));
            }

            var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
            var jwtTokenProvider        = new JwtTokenProvider();
            var token = jwtTokenProvider.GenerateJwtToken(user.ExternalId.ToString(), "ADMIN");

            var roles = new string[] { "CLIENT" };

            var userModel = new AuthenticatedUserInfoModel
            {
                Email     = user.Email,
                FirstName = user.FirstName,
                LastName  = user.LastName,
                UserName  = value.ssoID,
                Token     = jwtSecurityTokenHandler.WriteToken(token),
                Roles     = roles,
                Expires   = token.ValidTo,
                Issued    = token.ValidFrom
            };

            return(Redirect(BaseUrl + "?ssoId=" + userModel.UserName + "&token=" + userModel.Token + "&culture=hr-HR"));
        }