Пример #1
0
        public long CreateDriver(string emailAddress, string password)
        {
            var dbdriver = tripDataContext.Drivers.FirstOrDefault(d => d.EmailAddress.ToLower() == emailAddress.ToLower());
            if (dbdriver == null)
            {
                var salt = _encryptionHelper.CreateSalt();
                var driver = new Driver()
                    {
                        EmailAddress = emailAddress.ToLower(),
                        Salt = salt,
                        Password = _encryptionHelper.Encrypt(password, salt)
                    };
                tripDataContext.Drivers.Add(driver);
                try
                {
                    tripDataContext.SaveChanges();
                }
                catch (Exception ex)
                {
                    Trace.Write(ex.Message);
                }

                return driver.DriverId;
            }
            else
            {
                throw new InvalidOperationException("Driver already exists");
            }
        }
Пример #2
0
 public DriverPrincipal(Driver driver, string authenticationType, bool authenticated)
 {
     if (driver == null)
     {
         throw new ArgumentNullException("driver");
     }
     Identity = new Identity(driver , authenticationType, authenticated);
     DriverId = driver.DriverId;
 }
Пример #3
0
        public static void SetUseronThread(Driver driver)
        {
            var principal = GetDriverPrincipal(driver);

            //  Principal needs to be set on both Thread.CurrentPrincipal and HttpContext.Current.User
            //  http://stackoverflow.com/questions/12028604/how-can-i-safely-set-the-user-principal-in-a-custom-webapi-httpmessagehandler
            Thread.CurrentPrincipal = principal;
            if (HttpContext.Current != null)
            {
                HttpContext.Current.User = principal;
            }
        }
Пример #4
0
        public long CreateDriver(Driver driver)
        {
            var repository = ResolveDriverRepository();
            if (!repository.Exists(d => d.DriverId == driver.DriverId || d.EmailAddress.ToLower() == driver.EmailAddress.ToLower()))
            {
                driver.Salt = _encryptionHelper.CreateSalt();
                driver.Password = _encryptionHelper.Encrypt(driver.Password, driver.Salt);
                var newDriver = repository.Add(driver);

                return newDriver.DriverId;
            }
            else
            {
                throw new InvalidOperationException("Driver already exists");
            }
        }
Пример #5
0
        // POST /driver
        //    [RequireBasicAuthentication]
        public HttpResponseMessage Post(Driver driver)
        {
            if (ModelState.IsValid)
            {
                driver.DriverId = _driverRepository.UpdateDriver(driver);

              //  var response = new HttpResponseMessage<Driver>(driver, HttpStatusCode.Created);
                var response = Request.CreateResponse<Driver>(HttpStatusCode.Created, driver);

                string uri = Url.Link(null, new { id = driver.DriverId });
                response.Headers.Location = new Uri(Request.RequestUri, uri);

                return response;
            }

            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }
Пример #6
0
 public static BasePrincipal GetDriverPrincipal(Driver driver)
 {
     return new DriverPrincipal(driver, "Basic authentication", true);
 }
Пример #7
0
 public Identity(Driver account, string authenticationType, bool authentiated)
 {
     _account = account;
     _authenticationType = authenticationType;
     _authenticated = authentiated;
 }
Пример #8
0
 public long UpdateDriver(long id, Driver driver)
 {
     var repository = ResolveDriverRepository();
     if (repository.Exists(d => d.DriverId == id))
     {
         repository.Update(driver);
         return driver.DriverId;
     }
     else
     {
         throw new IdentityNotMappedException("Driver does not exist");
     }
 }
Пример #9
0
        public long UpdateDriver(Driver driver)
        {
            var dbDriver = tripDataContext.Drivers.Single(d => d.DriverId == driver.DriverId);
               dbDriver = driver;

               tripDataContext.SaveChanges();

            return driver.DriverId;
        }