public IHttpActionResult Post([FromBody] Register model) { LoginResponse loginResponse = new LoginResponse(); var rndNum = new Random().Next(999999999); var saved = 0; model.RequestId = rndNum.ToString(); Audit audit = new Audit { Activity = "Register", TimeStamp = DateTime.Now, RequestId = rndNum.ToString(), UserHostName = GetComputerDetails.GetHostName(), UserIPAddress = GetComputerDetails.GetIPAddress() }; if (!ModelState.IsValid) { loginResponse.StatusCode = "03"; loginResponse.StatusResponse = "Invalid model"; return(BadRequest(ModelState)); } using (ApplicationDbContext db = new ApplicationDbContext()) { var regExist = db.Registers.Any(x => x.Email == model.Email); if (regExist) { loginResponse.StatusCode = "08"; loginResponse.StatusResponse = "User already exist"; audit.Comment = "User already exist"; } else { model.TimeStamp = DateTime.Now; model.Password = Encryptor.EncryptAesManaged(model.Password); db.Registers.Add(model); try { saved = db.SaveChanges(); } catch (Exception ex) { audit.Comment = ex.Message; } if (saved > 0) { audit.Success = true; audit.Comment = "Success"; loginResponse.StatusCode = "00"; loginResponse.StatusResponse = "Successful"; loginResponse.Username = model.UserName; loginResponse.Name = model.FirstName + " " + model.LastName; } else { loginResponse.StatusCode = "04"; loginResponse.StatusResponse = "Failed"; audit.Comment = "Failed"; } } audit.TimeStamp = DateTime.Now; db.Audits.Add(audit); db.SaveChanges(); } return(Ok(loginResponse)); }
public IHttpActionResult Post([FromBody] Login model) { LoginResponse loginResponse = new LoginResponse(); var rndNum = new Random().Next(999999999); var login = new Login { RequestId = rndNum.ToString(), Password = Encryptor.EncryptAesManaged(model.Password), TimeStamp = DateTime.Now, Email = model.Email, }; Register reg = new Register(); // var hashedPassword = new PasswordHasher().HashPassword(model.Password); var hashedPassword = Encryptor.EncryptAesManaged(model.Password); Audit audit = new Audit { Activity = "Login", RequestId = rndNum.ToString(), UserHostName = GetComputerDetails.GetHostName(), UserIPAddress = GetComputerDetails.GetIPAddress() }; if (!ModelState.IsValid) { loginResponse.StatusCode = "03"; loginResponse.StatusResponse = "Invalid model"; return(BadRequest(ModelState)); } using (ApplicationDbContext db = new ApplicationDbContext()) { reg = db.Registers.FirstOrDefault(x => x.Email == model.Email && x.Password == hashedPassword); if (reg != null) { audit.Success = true; audit.Comment = "Success"; loginResponse.StatusCode = "00"; loginResponse.StatusResponse = "Found"; loginResponse.Username = model.Email; loginResponse.Name = reg.FirstName + " " + reg.LastName; login.Status = "Success"; } else { loginResponse.StatusCode = "02"; audit.Comment = "Wrong email and password combination"; loginResponse.StatusResponse = "Wrong email and password combination"; login.Status = "Failed"; } audit.TimeStamp = DateTime.Now; db.Logins.Add(login); db.Audits.Add(audit); db.SaveChanges(); } return(Ok(loginResponse)); }