public UserModel Authenticate(UserModel user) { // Gets a user from the database using the username and password. UserModel dbUser = _userDbContext.Get(user); // return null if user was not found. if (dbUser == null) { return(null); } // Authentication succeful; generate JWT token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_secretSettings.SecretString); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, dbUser.Id.ToString()) }), Expires = DateTime.Now.AddHours(8), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); dbUser.Token = tokenHandler.WriteToken(token); // Remove password before returning dbUser.Password = null; return(dbUser); }
public ActionResult <ProjectModel> Post([FromBody] ProjectModel project) { Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL"); project.TimeCreated = DateTime.Now; project.TimeLastEdit = DateTime.Now; if (project.Users == null) { project.Users = new List <UserRole>(); } // Get the UserId from the Claims. var user = User.Identity as ClaimsIdentity; var userId = user.FindFirst(ClaimTypes.Name)?.Value; if (userId != null) { var userModel = _usersDbContext.Get(userId); var userRole = new UserRole() { UserId = userId, Name = userModel.Username, Role = UserRole.UserRoleCreator }; project.Users.Add(userRole); } var result = _projectsDbContext.Post(project); return(Ok(result)); }
public UserModel Authenticate(UserModel user) { var dbUser = _userDbContext.Get(user); dbUser.Token = "testToken"; return(dbUser); }
public ActionResult <List <UserModel> > Get() { var user = _usersDbContext.Get(); return(Ok(user)); }