Пример #1
0
        public async Task <IHttpActionResult> PutUser(Models.UserModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await userRepository.GetOne(model.Id);

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


            user.FirstName    = model.FirstName;
            user.LastName     = model.LastName;
            user.UserName     = model.UserName;
            user.EmailAddress = model.EmailAddress;
            if (model.Password != null)
            {
                user.Salt = PasswordHasherService.GenerateSalt();
                user.Hash = PasswordHasherService.HashPassword(model.Password, user.Salt);
            }



            try
            {
                await userRepository.SaveAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!userRepository.UserExists(model.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #2
0
        public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin");

            if (allowedOrigin == null)
            {
                allowedOrigin = "*";
            }


            using (var db = new Models.APIContext())
            {
                var user = db.Users
                           .Where(u => u.UserName == context.UserName)
                           .FirstOrDefault();

                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return(Task.FromResult <object>(null));
                }

                if (user.Hash != PasswordHasherService.HashPassword(context.Password, user.Salt))
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return(Task.FromResult <object>(null));
                }

                var identity = GetIdentityFromUser(db, user, context.Options.AuthenticationType);

                var ticket = new AuthenticationTicket(identity, BuildTicketProperties(db, user, context.ClientId == null ? String.Empty : context.ClientId));
                context.Validated(ticket);

                return(Task.FromResult <object>(null));
            }
        }
Пример #3
0
        public async Task <IHttpActionResult> PostUser(Models.UserModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }



            var user = new User();

            user.FirstName    = model.FirstName;
            user.LastName     = model.LastName;
            user.UserName     = model.UserName;
            user.EmailAddress = model.EmailAddress;
            user.Salt         = PasswordHasherService.GenerateSalt();
            user.Hash         = PasswordHasherService.HashPassword(model.Password, user.Salt);

            userRepository.Add(user);

            await userRepository.SaveAsync();

            return(Created(Request.RequestUri + user.Id.ToString(), user));
        }
Пример #4
0
        protected override void Seed(API.Models.APIContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data. E.g.

            var salt = PasswordHasherService.GenerateSalt();

            context.Users.AddOrUpdate(
                p => p.UserName,
                new Core.User
            {
                EmailAddress = "*****@*****.**",
                FirstName    = "Admin",
                LastName     = "Test",
                Hash         = PasswordHasherService.HashPassword("test", salt),
                Salt         = salt,
                UserName     = "******"
            }
                );

            context.Policies.AddOrUpdate(p => p.Id,
                                         new Core.Policy
            {
                Id                 = 1,
                CustomerName       = "Grecia Hung",
                Description        = "Indernizacion del patrimonio",
                TypeCover          = Core.TypeCover.Earthquake,
                TypeRisk           = Core.TypeRisk.Low,
                PercentageCoverage = 20,
                Price              = 1000,
                StartDate          = DateTimeOffset.Now,
                EndDate            = DateTimeOffset.Now.AddYears(1)
            },
                                         new Core.Policy
            {
                Id                 = 2,
                CustomerName       = "Osner Sanchez",
                Description        = "N/A",
                TypeCover          = Core.TypeCover.Stole,
                TypeRisk           = Core.TypeRisk.Low,
                PercentageCoverage = 100,
                Price              = 5000,
                StartDate          = DateTimeOffset.Now,
                EndDate            = DateTimeOffset.Now.AddYears(1)
            },
                                         new Core.Policy
            {
                Id                 = 3,
                CustomerName       = "Juan Guaidó",
                Description        = "Aplica para perdida total",
                TypeCover          = Core.TypeCover.Fire,
                TypeRisk           = Core.TypeRisk.High,
                PercentageCoverage = 10,
                Price              = 10000,
                StartDate          = DateTimeOffset.Now.AddYears(-1),
                EndDate            = DateTimeOffset.Now.AddDays(-1)
            },
                                         new Core.Policy
            {
                Id                 = 4,
                CustomerName       = "Darwin Ruiz",
                Description        = "Aplica para perdida total",
                TypeCover          = Core.TypeCover.Others,
                TypeRisk           = Core.TypeRisk.Medium,
                PercentageCoverage = 10,
                Price              = 3000,
                StartDate          = DateTimeOffset.Now,
                EndDate            = DateTimeOffset.Now.AddDays(50)
            },
                                         new Core.Policy
            {
                Id                 = 5,
                CustomerName       = "Ambar Urbaez",
                Description        = "N/A",
                TypeCover          = Core.TypeCover.Earthquake,
                TypeRisk           = Core.TypeRisk.High,
                PercentageCoverage = 20,
                Price              = 10000,
                StartDate          = DateTimeOffset.Now,
                EndDate            = DateTimeOffset.Now.AddYears(1)
            },
                                         new Core.Policy
            {
                Id                 = 6,
                CustomerName       = "Davis Mejias",
                Description        = "N/A",
                TypeCover          = Core.TypeCover.Stole,
                TypeRisk           = Core.TypeRisk.Low,
                PercentageCoverage = 100,
                Price              = 5000,
                StartDate          = DateTimeOffset.Now,
                EndDate            = DateTimeOffset.Now.AddYears(1)
            },
                                         new Core.Policy
            {
                Id                 = 7,
                CustomerName       = "Andres Padilla",
                Description        = "N/A",
                TypeCover          = Core.TypeCover.Fire,
                TypeRisk           = Core.TypeRisk.Low,
                PercentageCoverage = 60,
                Price              = 10000,
                StartDate          = DateTimeOffset.Now.AddYears(-1),
                EndDate            = DateTimeOffset.Now.AddDays(-1)
            },
                                         new Core.Policy
            {
                Id                 = 8,
                CustomerName       = "Ibsen Rios",
                Description        = "Aplica para perdida total",
                TypeCover          = Core.TypeCover.Lost,
                TypeRisk           = Core.TypeRisk.MediumHigh,
                PercentageCoverage = 35,
                Price              = 9500,
                StartDate          = DateTimeOffset.Now,
                EndDate            = DateTimeOffset.Now.AddDays(100)
            }
                                         );
        }
Пример #5
0
        public async Task <IActionResult> RegisterAsync(RegisterViewModel registerViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            User user = new User()
            {
                UserPassword  = service.HashPassword(null, registerViewModel.Password),
                UserEmail     = registerViewModel.Email,
                UserFirstname = registerViewModel.Prenom,
                UserName      = registerViewModel.Nom,
                UserPoleId    = registerViewModel.PoleId,
                UserPhone     = registerViewModel.PhoneNumber,
                UserNumpermis = registerViewModel.NumPermis
            };

            //Vérification de l'email
            IdentityResult result = _authService.VerifUser(user, registerViewModel.Password);

            if (!result.Succeeded)
            {
                ModelState.AddModelError("Error", result.Errors.First().Description);

                return(BadRequest(ModelState));
            }

            // Ajout du rôle à l'utilisateur
            IdentityResult result2 = _authService.AddToRoleUserAsync(user);

            if (!result2.Succeeded)
            {
                ModelState.AddModelError("Error", result2.Errors.First().Description);
                return(BadRequest(ModelState));
            }

            // verification de l'unicité du téléphone
            IdentityResult result3 = _authService.VerifPhoneNumber(user);

            if (!result3.Succeeded)
            {
                ModelState.AddModelError("Error", result3.Errors.First().Description);
                return(BadRequest(ModelState));
            }


            // mise à jour de l'état du compte à en attente
            user.UserState = (sbyte)Enums.UserState.InWaiting;

            try
            {
                _context.User.Add(user);
                _context.SaveChanges();
                _context.Dispose();
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", "Une erreur est survenue." + e.Message);
                Console.WriteLine(e);
                return(BadRequest(ModelState));
            }

            // token valide 10 mins
            var tokenGenerate = TokenService.GenerateToken(user);

            string myFiles = System.IO.File.ReadAllText(ConstantsEmail.RegisterPath);

            myFiles = myFiles.Replace("%%LIEN%%", Environment.GetEnvironmentVariable("UrlVerifEmail") + tokenGenerate);
            myFiles = myFiles.Replace("%%USERNAME%%", user.UserFirstname);

            try
            {
                var response =
                    await EmailService.SendEmailAsync("Confirmez votre e-mail - BookYourCar", myFiles, user.UserEmail);
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error",
                                         "Une erreur s'est produite sur l'envoi de mail." + e.Message);
                return(BadRequest(ModelState));
            }

            return(Ok());
        }