Exemple #1
0
        public ActionResult SaveOrUpdate(User model)
        {
            if (ModelState.IsValid)
            {
                using (MyDBContext dc = new MyDBContext())
                {
                    if (model.Id > 0) //Update
                    {
                        var original = dc.User.Where(a => a.Id == model.Id && a.IsCurier == true && a.Role != "admin").Include(x => x.UserDetails).FirstOrDefault();
                        if (original != null)
                        {
                            model.IsCurier = true;
                            model.Role     = "user";
                            model.Password = PassCrypt.EnryptString(model.Password);
                            dc.Entry(original).CurrentValues.SetValues(model);
                            dc.Entry(original.UserDetails).CurrentValues.SetValues(model.UserDetails);
                        }
                    }
                    else //Save
                    {
                        model.IsCurier = true;
                        model.Role     = "user";
                        model.Id       = 0;
                        model.Password = PassCrypt.EnryptString(model.Password);
                        dc.User.Add(model);
                    }
                    dc.SaveChanges();
                }
            }

            return(View("Index"));
        }
Exemple #2
0
        public ActionResult LogIn(User model)
        {
            User findAdmin = new User();

            using (var context = new MyDBContext())
            {
                string pass        = "******";
                string encryptPass = PassCrypt.EnryptString(pass);
                findAdmin = context.User.Where(x => x.Email == "*****@*****.**" && x.Password == encryptPass && x.Role == "admin").FirstOrDefault();
            }
            if (model.Email == findAdmin.Email && PassCrypt.EnryptString(model.Password) == findAdmin.Password)
            {
                try
                {
                    var identity = new ClaimsIdentity(new[] {
                        new Claim(ClaimTypes.Email, findAdmin.Email),
                        new Claim(ClaimTypes.Role, findAdmin.Role),
                        new Claim(ClaimTypes.UserData, findAdmin.Id.ToString()),
                    }, "ApplicationCookie");

                    var ctx         = Request.GetOwinContext();
                    var authManager = ctx.Authentication;

                    authManager.SignIn(identity);

                    return(Redirect(GetRedirectUrl(model.ReturnUrl)));
                }
                catch (Exception) { }
            }

            //auth fail
            ModelState.AddModelError("Error", "Username sau parola incorecte");
            return(View());
        }
        public User ResetPass(string email)
        {
            //check if email in database
            var Users        = _context.User.ToList();
            var isInDatabase = false;

            foreach (var us in Users)
            {
                if (us.Email == email)
                {
                    isInDatabase = true;
                }
            }

            if (isInDatabase == false)
            {
                return(new User());
            }
            else
            {
                var account = _context.User.FirstOrDefault(t => t.Email == email);



                //decriptam parola veche
                account.OldPassword = PassCrypt.DecryptString(account.Password);
                string ResetCode = Guid.NewGuid().ToString("N").Substring(0, 10);

                //schimbam parola veche cu una noua criptata
                account.Password = PassCrypt.EnryptString(ResetCode);
                _context.User.Update(account);
                _context.SaveChanges();

                int x = SendResetPasswordEmail(email, ResetCode);

                account.UserDetails = null;

                if (account.UserDetails != null)
                {
                    account.UserDetails.User = null;
                }
                account.ClientUserRel         = null;
                account.CourierUserRel        = null;
                account.ChatMessages          = null;
                account.CourierLocation       = null;
                account.ContactMessages       = null;
                account.ReportProblemMessages = null;
                account.DocImages             = null;

                account.Password = ResetCode;
                return(account);
            }
        }
        public User LoginCourier(string email, string password)
        {
            string cryptPass = PassCrypt.EnryptString(password);

            var item = _context.User.Where(t => t.Email == email && t.Password == cryptPass && t.IsCurier == true).Include(x => x.UserDetails).FirstOrDefault();

            if (item != null)
            {
                item.Password         = PassCrypt.DecryptString(cryptPass);
                item.UserDetails.User = null;
            }
            return(item);
        }
        public IActionResult ChangePassword(long id, string password)
        {
            var todo = _context.User.Where(t => t.Id == id).FirstOrDefault();

            if (todo == null)
            {
                return(NotFound());
            }

            todo.Password = PassCrypt.EnryptString(password);


            _context.User.Update(todo);
            _context.SaveChanges();
            return(new NoContentResult());
        }
        public IActionResult Create([FromBody] User user)
        {
            if (user == null)
            {
                return(BadRequest());
            }

            var Users = _context.User.ToList();

            foreach (var us in Users)
            {
                if (us.Email == user.Email)
                {
                    return(BadRequest("Acest email este deja asignat altui cont !"));
                }
            }

            user.Role     = "User";
            user.IsCurier = false;
            user.Password = PassCrypt.EnryptString(user.Password);

            _context.User.Add(user);
            _context.SaveChanges();


            if (user.UserDetails != null)
            {
                user.UserDetails.User = null;
            }
            user.ClientUserRel         = null;
            user.CourierUserRel        = null;
            user.ChatMessages          = null;
            user.CourierLocation       = null;
            user.ContactMessages       = null;
            user.ReportProblemMessages = null;
            user.DocImages             = null;

            return(Ok(user));
        }