public async void ShouldNotCreateAuthenticatedUserWhenUserDisabled() { var options = new DbContextOptionsBuilder <TravelExpensesContext>() .UseInMemoryDatabase(databaseName: nameof(ShouldNotCreateAuthenticatedUserWhenUserDisabled)) .Options; // Insert seed data into the database using one instance of the context using (var context = new TravelExpensesContext(options)) { context.Users.Add(new User { Id = 1, Email = "*****@*****.**", PasswordHash = "$2y$12$yVYkJsR7a4Wj3wRzCD9Pn.DvDGWY3Dzx2AwisSqailn3Pyu.X.zWi", //password Disabled = true }); context.SaveChanges(); } using (var context = new TravelExpensesContext(options)) { var loginDetails = new UserIn( "CaptainBedpan", "*****@*****.**", "password"); var sut = CreateHandler(loginDetails, context); var authenticatedUser = await sut.Handle( new Query(loginDetails), CancellationToken.None); authenticatedUser.ShouldBeNull(); } }
public IActionResult Authenticate([FromBody] UserIn userParam) { UserOut authenticatedUser = userService.Authenticate(userParam); if (authenticatedUser == null) { return(BadRequest(new { message = "Username or password is incorrect" })); } return(Ok(authenticatedUser)); }
private Handler CreateHandler(UserIn loginDetails, TravelExpensesContext context) { var config = new MapperConfiguration(cfg => cfg.CreateMap <User, AuthenticatedUserOut>()); var mapper = config.CreateMapper(); var tokenGenerator = new Mock <ITokenGenerator>(); tokenGenerator.Setup(tg => tg.CreateTokenString(It.IsAny <User>())).Returns("token"); return(new Handler( context, mapper, tokenGenerator.Object)); }
public async Task <IActionResult> Authenticate([FromBody] UserIn userParam) { if (string.IsNullOrEmpty(userParam.Password) || string.IsNullOrEmpty(userParam.Email)) { return(BadRequest(new ErrorDetails { Message = InvalidCredsMsg })); } var authenticatedUser = await mediator.Send(new GetAuthenticatedUser.Query(userParam)).ConfigureAwait(false); if (authenticatedUser == null) { return(BadRequest(new ErrorDetails { Message = InvalidCredsMsg })); } return(Ok(authenticatedUser)); }
public UserOut Authenticate(UserIn userIn) { var user = _users.SingleOrDefault(x => x.Email == userIn.Email); // return null if user not found if (user == null) { return(null); } bool passMatchesHash = BCrypt.Net.BCrypt.Verify(userIn.Password, user.PasswordHash); // return null when wrong password if (!passMatchesHash) { return(null); } // authentication successful so generate jwt token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.Id.ToString()) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var userOut = mapper.Map <UserOut>(user); userOut.Token = tokenHandler.WriteToken(token); return(userOut); }
void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) { //Trace.WriteLine("UserLists.Users " + UserLists.Users.Count); using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame()) { if (bodyFrame != null) { bodyFrame.GetAndRefreshBodyData(UserManager.bodies); List <ulong> trackedIds = new List <ulong>(); foreach (Body body in UserManager.bodies) { if (body == null) { continue; } if (body.IsTracked) { trackedIds.Add(body.TrackingId); } } List <ulong> knownIds = new List <ulong>(UserManager.Users.Keys); foreach (ulong id in knownIds) { if (!trackedIds.Contains(id)) { if (LastFrameBodyCount == 1) { //invoke no user NoUser.Invoke(null, new UserEventArg(id)); } else { LostUser.Invoke(null, new UserEventArg(id));//invoke lost user } UserManager.Users.Remove(id); } } for (int i = 0; i < UserManager.bodies.Length; i++) { if (UserManager.bodies[i] == null) { continue; } if (UserManager.bodies[i].IsTracked) { if (!UserManager.Users.ContainsKey(UserManager.bodies[i].TrackingId)) { UserManager.Users[UserManager.bodies[i].TrackingId] = i; if (LastFrameBodyCount == 0) { //Invoke user in UserIn.Invoke(null, new UserEventArg(UserManager.bodies[i].TrackingId)); } else { //Invoke found new user NewUser.Invoke(null, new UserEventArg(UserManager.bodies[i].TrackingId)); } } //Trace.WriteLine(body.TrackingId); } } if (UserManager.Users.Count > 0) { gestureDetector.IsPaused = false; ulong closestUser = ChooseClosestSkeletons(UserManager.bodies, 1); if (UserManager.closestUser != closestUser) { UserManager.closestUser = closestUser; } bool isInteractive = UserManager.IsMainUserInteractive; if (UserManager.currentUser != gestureDetector.TrackingId) { gestureDetector.TrackingId = UserManager.currentUser; } if (isInteractive != LastInteractive) { if (isInteractive) { StateControl.SwitchState(ProgramState.H1_G1_I1); } else { StateControl.SwitchState(ProgramState.H1_G0_I0); } LastInteractive = isInteractive; } //updateinterval++; //if (updateinterval >= 60) //{ // foreach (var bd in UserManager.bodies) // { // if (bd.TrackingId != 0) // sqlUpdateUser(bd.TrackingId); // } // updateinterval = 0; //} } else { gestureDetector.IsPaused = true; } int maxBodies = kinectSensor.BodyFrameSource.BodyCount; for (int i = 0; i < maxBodies; i++) //control pause and resume hand raise detector { Body body = UserManager.bodies[i]; ulong trackingId = body.TrackingId; if (trackingId != handRaiseList[i].trackingId) { handRaiseList[i].trackingId = trackingId; handRaiseList[i].isPaused = trackingId == 0; } handRaiseList[i].Update(); } if (UserManager.currentUser != 0 && UserManager.IsMainUserInteractive) //move mouse control if interactive { handPointer.Update(); } LastFrameBodyCount = UserManager.Users.Count; if (ConfigParams.UserView != LastUserView) { if (ConfigParams.UserView) { main.UserView.Opacity = 100; } else { main.UserView.Opacity = 0; } LastUserView = ConfigParams.UserView; } if (ConfigParams.UserView) { userView.DrawBodies(); } } } }
public Query(UserIn loginDetails) { LoginDetails = loginDetails; }
public Application(UserIn input, UserOut output) { this.input = input; this.output = output; }
public async Task <IActionResult> UpdateUserAsync([FromRoute(Name = "userName")] string userName, [FromBody] UserIn userIn) { try { var user = await _userService.UpdateUserAsync(new User { Email = userIn.Email, Username = userName }, userIn.Password).ConfigureAwait(false); return(Ok(user)); } catch (UserUpdateException e) { return(BadRequest(e.Errors)); } catch (UserAdminException) { return(Unauthorized("User admin cannot be updated.")); } }
public async Task <IActionResult> CreateUserAsync([FromRoute(Name = "userName")] string userName, [FromBody] UserIn userIn) { try { var user = await _userService.CreateUserAsync(new User { Email = userIn.Email, Username = userName }, userIn.Password).ConfigureAwait(false); return(Created($"{Request.Path}/{userName}", user)); } catch (UserCreateException e) { return(BadRequest(e.Errors)); } catch (UserAlreadyExistException) { return(Conflict()); } }