public bool Edit(SM_USER user) { try { SM_USER editedUser = DB.SM_USER.SingleOrDefault(u => u.ID_User == user.ID_User); if (!user.TX_Email.Equals(editedUser.TX_Email)) { return(false); } editedUser.TX_Email = user.TX_Email; editedUser.TX_FirstName = user.TX_FirstName; editedUser.TX_SecondName = user.TX_SecondName; editedUser.TX_LastName = user.TX_LastName; editedUser.TX_SecondLastName = user.TX_SecondLastName; editedUser.TX_Phone = user.TX_Phone; if (user.PasswordChanged) { editedUser.TX_Password = HashHandler.CreateHash(user.TX_Password); } DB.SaveChanges(); return(true); } catch (Exception ex) { return(false); } }
public IActionResult Login(LoginRequestDto request, [FromServices] IStudentsDbService isdbs) { var salt = isdbs.getSalt(request.Eska); var passw = HashHandler.CreateHash(request.Haslo, salt); using (var con = new SqlConnection("Data Source=db-mssql;Initial Catalog=s18309;Integrated Security=True")) using (var com = new SqlCommand()) { com.Connection = con; com.CommandText = ("select 1 from Student where IndexNumber = @index AND Password = @Pass"); com.Parameters.AddWithValue("Pass", passw); com.Parameters.AddWithValue("index", request.Eska); con.Open(); var dr = com.ExecuteReader(); if (!dr.Read()) { return(BadRequest("Wrong login or password")); } } //=----------------------------------------------------------------------------- var claims = new[] { new Claim(ClaimTypes.NameIdentifier, "1"), new Claim(ClaimTypes.Name, "1"), new Claim(ClaimTypes.Role, "employee"), new Claim(ClaimTypes.Role, "student") }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("DefinietlyNotASecretKeyasd213qwsdeq234123saw")); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( issuer: "Gakko", audience: "Students", claims: claims, expires: DateTime.Now.AddMinutes(10), signingCredentials: creds ); var refreshTokenik = Guid.NewGuid(); isdbs.SetREFRESHTOKEN(request.Eska, refreshTokenik.ToString()); return(Ok(new { token = new JwtSecurityTokenHandler().WriteToken(token), refreshToken = refreshTokenik })); }
public SM_USER Create(SM_USER user) { try { user.TX_Password = HashHandler.CreateHash(user.TX_Password); DB.SM_USER.Add(user); DB.SaveChanges(); return(DB.SM_USER.OrderByDescending(x => x.ID_User).FirstOrDefault()); } catch (Exception ex) { return(user); } }