public MyUser Authenticate_User(string userName) { if (!testUsersCredentials.ContainsKey(userName)) { throw new ArgumentOutOfRangeException("No credentials for such username!"); } LoginBM m = new LoginBM(); m.Username = userName; m.Password = testUsersCredentials[userName]; var stringContent = new StringContent(JsonConvert.SerializeObject(m), System.Text.Encoding.UTF8, "application/json"); var lgresponse = _client.PostAsync("/api/user/login", stringContent).Result; Assert.Equal(HttpStatusCode.OK, lgresponse.StatusCode); lgresponse.Headers.TryGetValues("set-cookie", out IEnumerable <string> s); List <string> s1 = new List <string>(s); _client.DefaultRequestHeaders.TryAddWithoutValidation("Cookie", s1[0]); return(new MyUser() { UserName = userName }); }
public async Task <IActionResult> DeleteAccount([FromBody] LoginBM credentials) { //check if username and password are valid strings: if (!ModelState.IsValid) { return(BadRequest()); } MyUser user = _context.Users.Cast <MyUser>().Single(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { return(BadRequest()); } //check if credentials are correct: if (!(await _userManager.CheckPasswordAsync(user, credentials.Password))) { return(Unauthorized()); } var result = await _userManager.DeleteAsync(user); if (!result.Succeeded) { return(BadRequest()); } return(Ok()); }
public LoginViewModel() { _loginBM = new LoginBM(); LoginCommand = new RelayCommand(param => LoginSession(), param => true); Message = ""; IsLoadingSession = false; _isConnecting = false; Mediator.Register("Retry_Login_UC", OnRetryListener); }
public ActionResult Login([Bind(Include = "Username,Password")] LoginBM user) { var httpCookie = this.Request.Cookies.Get("sessionId"); if (httpCookie != null && AuthenticationManager.IsAuthenticated(httpCookie.Value)) { return(this.RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { this.service.Login(user, Session.SessionID); this.Response.SetCookie(new HttpCookie("sessionId", Session.SessionID)); ViewBag.Username = user.Username; return(this.RedirectToAction("Index", "Home")); } return(View()); }
public async Task <IActionResult> Login([FromBody] LoginBM loginModel) { //if username and password are valid strings, try to login. Otherwise, return BadRequest. if (ModelState.IsValid) { var result = await _signInManager.PasswordSignInAsync(loginModel.Username, loginModel.Password, isPersistent : false, lockoutOnFailure : false); if (result.Succeeded) { return(Ok(new UserBasicVM(loginModel.Username))); } else { return(Unauthorized()); //valid but wrong credentials } } return(BadRequest()); //invalid credentials }
public void Login(LoginBM user, string sessionId) { if (!this.Contex.Sessions.Any(login => login.SessionId == sessionId)) { this.Contex.Sessions.Add(new Session() { SessionId = sessionId }); this.Contex.SaveChanges(); } var session = Contex.Sessions.FirstOrDefault(x => x.SessionId == sessionId); var userToLog = Contex.Users.FirstOrDefault(x => x.Username == user.Username && x.Password == user.Password); if (userToLog != null) { session.User = userToLog; session.IsActive = true; Contex.SaveChanges(); } }