public ActionResult RegisterCustomer(AccountCustomerViewModel _model) { ViewBag.PageNumber = 1; if (ModelState.IsValid) { using (BuilderDBEntities db = new BuilderDBEntities()) { var compEmail = db.Companies.FirstOrDefault(x => x.email == _model.email); var compLogin = db.Companies.FirstOrDefault(x => x.login == _model.login); var custEmail = db.Customers.FirstOrDefault(x => x.email == _model.email); var custLogin = db.Customers.FirstOrDefault(x => x.login == _model.login); if ((compEmail == null) && (compLogin == null) && (custEmail == null) && (custLogin == null)) { Customer customer = new Customer(); customer.name = _model.name; customer.surname = _model.surname; customer.login = _model.login; customer.phoneNumber = _model.phoneNumber; customer.email = _model.email; customer.password = Security.sha512encrypt(_model.password); customer.role_id = 1; customer.isDelete = false; db.Customers.Add(customer); db.SaveChanges(); return(RedirectToAction("Login")); } else if ((compEmail != null) || (custEmail != null)) { ModelState.AddModelError("Email", "Użytkownik o podanym emailu już istnieje"); } else if ((compLogin != null) || (custLogin != null)) { ModelState.AddModelError("Login", "Użytkownik o podanym loginie już istnieje"); } } } return(View(_model)); }
public ActionResult LoginCustomer(AccountCustomerViewModel _model) { using (BuilderDBEntities db = new BuilderDBEntities()) { bool validEmail = db.Customers.Any(x => x.email == _model.email); bool validLogin = db.Customers.Any(x => x.login == _model.login); if (!(validEmail || validLogin)) { ModelState.AddModelError("Password", "Niepoprawny login lub hasło"); return(View(_model)); } _model.password = Security.sha512encrypt(_model.password); Customer customer = db.Customers.FirstOrDefault(u => u.login.Equals(_model.password) && u.password.Equals(_model.password)); string authId = Guid.NewGuid().ToString(); Session["AuthID"] = authId; var cookie = new HttpCookie("AuthID"); cookie.Value = authId; Response.Cookies.Add(cookie); if (customer != null) { FormsAuthentication.SetAuthCookie(customer.login, false); var authTicket = new FormsAuthenticationTicket(1, customer.login, DateTime.UtcNow, DateTime.UtcNow.AddMinutes(60), false, ""); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)); authCookie.Expires = DateTime.UtcNow.AddMinutes(60); Response.SetCookie(authCookie); return(RedirectToAction("Home", "Account")); } return(View(_model)); } }