Esempio n. 1
0
        public HttpResponseMessage CreateAccount(LogonRequestModel logonModel)
        {
            if (ModelState.IsValid)
            {
                var driverId = _driverRepository.CreateDriver(logonModel.Email, logonModel.Password);
                if (driverId > 0)
                {
                    var driver = _driverRepository.GetDriverById(driverId);
                    if (driver.Token == null)
                    {
                        driver.Token = new AccessToken();
                    }
                    AccountHelper.SetToken(driver.Token, driverId);
                    _driverRepository.UpdateDriver(driver);
                    var responseMessage = Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, AccountHelper.GetLogonResponseModel(true, driver.Token.Token, driver.DriverId, driver.EmailAddress));
                    SecurityHelper.SetUseronThread(driver);

                    var cookie = new CookieHeaderValue(SecurityHelper.AccessTokenCookieName, driver.Token.Token);
                    cookie.Expires = DateTimeOffset.Now.AddDays(14);
                    cookie.Path = "/";

                    responseMessage.Headers.AddCookies(new CookieHeaderValue[] { cookie });

                    return responseMessage;
                }
                else
                {
                    return Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, AccountHelper.GetLogonResponseModel(false));
                }
            }

            return Request.CreateResponse<LogonResponseModel>(HttpStatusCode.BadRequest, AccountHelper.GetLogonResponseModel(false));
        }
Esempio n. 2
0
        public HttpResponseMessage Login(LogonRequestModel logonModel)
        {
            if (ModelState.IsValid)
            {
                var driver = _driverRepository.ValidateDriver(logonModel.Email, logonModel.Password);
                if (driver == null)
                {
                    return Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, AccountHelper.GetLogonResponseModel(false));
                }
                else
                {
                    if (driver.Token != null)
                    {
                        AccountHelper.SetToken(driver.Token, driver.DriverId);
                    }
                    else
                    {
                        var token = new AccessToken();
                        AccountHelper.SetToken(token, driver.DriverId);
                        driver.Token = token;
                    }
                    _driverRepository.UpdateDriver(driver);

                    long carId = 0;
                    if (driver.Cars != null)
                    {
                        var car = driver.Cars.FirstOrDefault(c => c.IsCurrentCar);

                        if (car != null)
                        {
                            carId = car.CarId;
                        }
                    }

                    var responseMessage = Request.CreateResponse<LogonResponseModel>(HttpStatusCode.OK, AccountHelper.GetLogonResponseModel(true,
                                                                                                                                            driver.Token.Token,
                                                                                                                                            driver.DriverId,
                                                                                                                                            driver.EmailAddress,
                                                                                                                                            driver.FirstName,
                                                                                                                                            driver.LastName,
                                                                                                                                            carId));
                    SecurityHelper.SetUseronThread(driver);

                    var cookie = new CookieHeaderValue(SecurityHelper.AccessTokenCookieName, driver.Token.Token);
                    cookie.Expires = DateTimeOffset.Now.AddDays(14);
                    cookie.Path = "/";

                    responseMessage.Headers.AddCookies(new CookieHeaderValue[] { cookie });

                    return responseMessage;
                }
            }

            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }