Example #1
0
        public ActionResult Login(LoginViewModel model)
        {
            string OldHASHValue = string.Empty;

            byte[] SALT = new byte[64];

            if (ModelState.IsValid)
            {
                var truckOwner = _context.TruckOwners.SingleOrDefault(t => t.Email == model.Email);
                if (truckOwner == null)
                {
                    ModelState.AddModelError("Email", "Given Email is not registered with us.");
                    return(View(model));
                }

                var login = _context.Logins.SingleOrDefault(l => l.Email == truckOwner.Email);
                if (login == null)
                {
                    ModelState.AddModelError("Email", "Given Email is not registered with us.");
                    return(View(model));
                }
                else if (!login.EmailActivated)
                {
                    ModelState.AddModelError("Email", "Email is not verified, please verify from the email sent.");
                    return(View(model));
                }

                OldHASHValue = login.Password;
                SALT         = login.PasswordSalt;

                bool isValidLogin = AuthenticationLogic.CompareHashValue(model.Password, model.Email, OldHASHValue, SALT);

                if (!isValidLogin)
                {
                    ModelState.AddModelError("Password", "Given password is incorrect.");
                    return(View(model));
                }

                FormsAuthentication.SignOut();
                // Write the authentication cookie
                FormsAuthentication.SetAuthCookie(truckOwner.Email, false);

                Session["DriverID"] = model.Email;

                return(RedirectToLocal(model.ReturnURL));
            }

            return(View(model));
        }
        public IHttpActionResult Login(AccountLoginDto model)
        {
            string OldHASHValue = string.Empty;

            byte[]        SALT   = new byte[64];
            List <string> errors = new List <string>();

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            switch (model.AccountType)
            {
            case "C":
                var customer = _context.Customers.SingleOrDefault(c => c.Email == model.Email);
                if (customer == null)
                {
                    ModelState.AddModelError("", "Given Email is not registered with us.");
                    return(BadRequest(ModelState));
                }

                var login = _context.Logins.SingleOrDefault(l => l.Email == customer.Email);
                if (login == null)
                {
                    ModelState.AddModelError("", "Given Email is not registered with us.");
                    return(BadRequest(ModelState));
                }
                else if (!login.EmailActivated)
                {
                    ModelState.AddModelError("", "Email is not verified, please verify from the email sent.");
                    return(BadRequest(ModelState));
                }
                OldHASHValue = login.Password;
                SALT         = login.PasswordSalt;

                bool isValidLogin = AuthenticationLogic.CompareHashValue(model.Password, model.Email, OldHASHValue, SALT);

                if (!isValidLogin)
                {
                    ModelState.AddModelError("", "Given password is incorrect.");
                    return(BadRequest(ModelState));
                }
                else
                {
                    var customerDto = Mapper.Map <Customer, CustomerDto>(customer);
                    customerDto.AverageRating = BusinessLogic.GetAverageCustomerRating(customerDto.CustomerId);
                    return(Ok(customerDto));
                }

            case "D":
                var truckOwner = _context.TruckOwners.SingleOrDefault(t => t.Email == model.Email);
                if (truckOwner == null)
                {
                    ModelState.AddModelError("", "Given Email is not registered with us.");
                    return(BadRequest(ModelState));
                }

                var truckOwnerLogin = _context.Logins.SingleOrDefault(l => l.Email == truckOwner.Email);
                if (truckOwnerLogin == null)
                {
                    ModelState.AddModelError("", "Given Email is not registered with us.");
                    return(BadRequest(ModelState));
                }
                else if (!truckOwnerLogin.EmailActivated)
                {
                    ModelState.AddModelError("", "Email is not verified, please verify from the email sent.");
                    return(BadRequest(ModelState));
                }

                OldHASHValue = truckOwnerLogin.Password;
                SALT         = truckOwnerLogin.PasswordSalt;

                bool isValid = AuthenticationLogic.CompareHashValue(model.Password, model.Email, OldHASHValue, SALT);

                if (!isValid)
                {
                    ModelState.AddModelError("", "Given password is incorrect.");
                    return(BadRequest(ModelState));
                }
                else
                {
                    var truckOwnerDto = Mapper.Map <TruckOwner, TruckOwnerDto>(truckOwner);
                    truckOwnerDto.AverageRating = BusinessLogic.GetAverageDriverRating(truckOwnerDto.TruckOwnerId);
                    return(Ok(truckOwnerDto));
                }

            default:
                ModelState.AddModelError("", "Invalid Account type");
                return(BadRequest(ModelState));
            }
        }