Пример #1
0
        public IActionResult Login([FromBody] ViewAuth viewAuth)
        {
            var auth = _userService.GetUser(viewAuth);

            if (auth == null)
            {
                return(Ok(new { msg = "用户名或密码不正确!" }));
            }

            return(Ok(new
            {
                Username = auth.Account,
                Token = _jwtService.GetToken(viewAuth)
            }));
        }
Пример #2
0
        public string GetToken(ViewAuth viewAuth)
        {
            var auth            = _userService.GetUser(viewAuth);
            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_configuration["SecurityKey"]);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, auth.Id.ToString()),
                    //new Claim(ClaimTypes.Role, user.UserRoles)
                }),
                Expires            = DateTime.UtcNow.AddDays(7),//有效时间
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            return(tokenString);
        }
Пример #3
0
        public IActionResult Login([FromBody] ViewAuth viewAuth)
        {
            try
            {
                var user = _iServiceUser.Login(viewAuth.Login, viewAuth.Password);
                if (user == null)
                {
                    return(BuildJsonResponse(404, "L'utilisateur n'existe pas", null, "Le login ou le password est incorrect"));
                }
                var Claims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),

                    //new Claim(JwtRegisteredClaimNames.Email , user.Email),
                };
                var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtSecurityToken:Key"]));
                var signingCredentials   = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha512);
                var jwtSecurityToken     = new JwtSecurityToken(
                    issuer: _configuration["JwtSecurityToken:Issuer"],
                    audience: _configuration["JwtSecurityToken:Audience"],
                    claims: Claims,
                    expires: DateTime.UtcNow.AddMonths(1),
                    signingCredentials: signingCredentials


                    );
                var data = new
                {
                    token       = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken),
                    expiration  = jwtSecurityToken.ValidTo,
                    currentUser = user,
                };

                return(BuildJsonResponse(200, "Authentification réussie", data));
            }

            catch (Exception e)
            {
                return(BuildJsonResponse(500, "Erreur serveur", null, e.Message));
            }
        }
Пример #4
0
 public User GetUser(ViewAuth auth)
 => Function(context => context
             .Users.Include(x => x.UserRoles)
             .FirstOrDefault(u =>
                             u.Account == auth.Account
                             & u.Password == auth.Password));