public async Task <ActionResult> Login([FromBody] DTO.LoginModel loginModel) { // Depuis ASP.NET Core 2.1, ces deux lignes sont superflues. // En effet, le framework prend en charge le retour d'une erreur 400 // incluant le détail de cette dernière si la validation ne s'est pas bien déroulée. // Il est possible de désactiver ce mode de fonctionnement. Pour plus d'informations, voir https://docs.microsoft.com/en-us/aspnet/core/web-api/index?view=aspnetcore-2.1#automatic-http-400-responses // if (!ModelState.IsValid) // return BadRequest(ModelState); var repository = new AuthenticationRepository(); Model.User userFound = repository.GetUsers().FirstOrDefault(user => user.UserName == loginModel.UserName && user.Password == loginModel.Password); if (userFound == null) { return(Unauthorized()); } var claims = new List <Claim> { new Claim(JwtRegisteredClaimNames.Sub, userFound.UserName), new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()), new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64), new Claim(PrivateClaims.UserId, userFound.Id.ToString()) }; // rappel: le token est configurable. On y ajoute ce que l'on veut comme claims! // un ensemble de nom de claims est "réservé" (voir JwtRegisteredClaimNames) // le reste est utilisable à loisir! Voir classe PrivateClaims par exemple. if (userFound.Roles != null) { userFound.Roles.ToList().ForEach(roleName => claims.Add(new Claim("roles", roleName))); } //IEnumerable<string> roles = await _userManager.GetRolesAsync(user); JwtSecurityToken token = new JwtSecurityToken( issuer: _jwtOptions.Issuer, audience: _jwtOptions.Audience, claims: claims, notBefore: _jwtOptions.NotBefore, expires: _jwtOptions.Expiration, signingCredentials: _jwtOptions.SigningCredentials ); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(token); // Sérialisation et retour var response = new { access_token = encodedJwt, expires_in = (int)_jwtOptions.ValidFor.TotalSeconds }; return(Ok(response)); }
public async Task <IActionResult> Login([FromBody] DTO.LoginModel loginModel, bool adminConnection = false) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } loginModel.Login = loginModel.Login.Trim(); loginModel.Password = loginModel.Password.Trim(); Model.User userFound = await(new UserDataAccess(Context)).FindByStrictLoginAsync(loginModel.Login); bool isFoundAndEnabled = userFound != null && userFound.IsEnabled; if (adminConnection) { if (!isFoundAndEnabled || userFound.Roles != Model.Constants.Roles.ADMIN || userFound.Roles != Model.Constants.Roles.GESTIONNARY) { return(NotFound()); } } else if (!isFoundAndEnabled) { return(NotFound()); } List <string> data = userFound.Password.Split('.').ToList(); (string hashedCheck, string salt) = (data.ElementAt(0), data.ElementAt(1)); string hashedToVerify = (await API.Services.HashPassword.HashAsync(loginModel.Password, salt)).hashed; if (!hashedToVerify.Equals(hashedCheck)) { return(BadRequest(new DTO.Error { Message = "The login or password is wrong" })); } JwtSecurityToken token = await CreateToken(userFound); return(Ok( new DTO.Token { access_token = new JwtSecurityTokenHandler().WriteToken(token), expires_in = (int)_jwtOptions.ValidFor.TotalSeconds } )); }