async Task LoginToMenu() { var user = new UserToLoginDto { UserName = Login, Password = this.Password }; try { LoggedUserDto loggedUser = await _api.Login(user); if (loggedUser.UserType == "P") { ShowViewModel <MenuPoliceViewModel>(); _userDialogs.Alert("Logged as Police"); } else if (loggedUser.UserType == "U") { ShowViewModel <MenuViewModel>(); _userDialogs.Alert("Logged as User"); } else { ShowViewModel <MenuPoliceViewModel>(); //_userDialogs.Alert("Logged as Admin"); } } catch (Exception ex) { _userDialogs.Alert(ex.Message); } }
public void ValidUserCredentialsAndStatus_ReturnLoggedUser() { var loggedUser = new LoggedUserDto(); _commonService.Setup(x => x.CreateLoggedUser(_user)) .Returns(loggedUser); var result = _sut.Handle(_query, default).Result; Assert.That(result.LoggedUser, Is.EqualTo(loggedUser)); }
public HttpResponseMessage LoginUser(UserDto value) { BloggingSystemContext context = null; try { context = new BloggingSystemContext(); this.ValidateUserIdentifier( value.Username, "Username", MinUsernameLength, MaxUsernameLength, ValidUsernameCharacters); this.ValidateAuthCode(value.AuthCode); var user = context.Users.FirstOrDefault( u => u.Username == value.Username && u.AuthCode == value.AuthCode); if (user == null) { throw new InvalidOperationException("Invalid username or password."); } if (user.SessionKey == null || user.SessionKey.Length != SessionKeyLength) { user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); } var loggedUserDto = new LoggedUserDto() { DisplayName = user.DisplayName, SessionKey = user.SessionKey }; var response = Request.CreateResponse(HttpStatusCode.Accepted, loggedUserDto); return(response); } catch (Exception ex) { var errorResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message); throw new HttpResponseException(errorResponse); } finally { if (context != null) { context.Dispose(); } } }
public string TokenBuild(LoggedUserDto loggedUserDto) { string secureToken = BuildSecureToken(); _context.Tokens.Add(new Token { UserId = loggedUserDto.UserId, SecureToken = secureToken, CreateDate = DateTime.Now }); _context.SaveChanges(); return(secureToken); }
public void TestLogoutUser_SessionKeyIsNull_ShouldReturnOK() { var loggedUser = new LoggedUserDto { DisplayName = "Peter Petroff", SessionKey = null }; var headers = new Dictionary <string, string>(); headers["X-SessionKey"] = loggedUser.SessionKey; var logoutResult = httpServer.Put("api/users/logout", headers); Assert.AreEqual(HttpStatusCode.BadRequest, logoutResult.StatusCode); }
public async Task <IActionResult> Login(LoggedUserDto newLoggedUser) { var userInDb = await _repos.Login(newLoggedUser.Username.ToLower(), newLoggedUser.Password); if (userInDb == null) { return(Unauthorized()); } // If user is valid starts to build the JWT (Json Web Token) // Define claims var claims = new[] { new Claim(ClaimTypes.NameIdentifier, userInDb.Id.ToString()), new Claim(ClaimTypes.Name, userInDb.Username) }; // Get private Key var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value)); // Encrypt private key var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature); // Create token var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddDays(1), SigningCredentials = credentials }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); return(Ok(new { token = tokenHandler.WriteToken(token) })); }
public HttpResponseMessage RegisterUser(UserDto value) { BloggingSystemContext context = null; try { context = new BloggingSystemContext(); this.ValidateUserIdentifier( value.Username, "Username", MinUsernameLength, MaxUsernameLength, ValidUsernameCharacters); this.ValidateUserIdentifier( value.DisplayName, "Display name", MinDisplayNameLength, MaxDisplayNameLength, ValidDisplayNameCharacters); this.ValidateAuthCode(value.AuthCode); var user = context.Users.FirstOrDefault( u => u.Username == value.Username || u.DisplayName == value.DisplayName); if (user != null) { throw new InvalidOperationException("User already exists."); } user = new User() { Username = value.Username, DisplayName = value.DisplayName, AuthCode = value.AuthCode }; context.Users.Add(user); context.SaveChanges(); user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); var loggedUserDto = new LoggedUserDto() { DisplayName = user.DisplayName, SessionKey = user.SessionKey }; var response = Request.CreateResponse(HttpStatusCode.Created, loggedUserDto); return(response); } catch (Exception ex) { var errorResponse = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message); throw new HttpResponseException(errorResponse); } finally { if (context != null) { context.Dispose(); } } }