Ejemplo n.º 1
0
        public async Task <OperationDetails> Create(UserTransferModel userDto)
        {
            UserSecurity user = await Database.UserSecurityManager.FindByEmailAsync(userDto.Email);

            if (user == null)
            {
                user = new UserSecurity {
                    Email = userDto.Email, UserName = userDto.Email
                };
                var result = await Database.UserSecurityManager.CreateAsync(user, userDto.Password);

                if (result.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
                }

                // add a role
                await Database.UserSecurityManager.AddToRoleAsync(user.Id, userDto.Role);

                // create user profile
                //ClientProfile clientProfile = new ClientProfile { Id = user.Id, Address = userDto.Address, Name = userDto.Name };
                //Database.ClientManager.Create(clientProfile);

                await Database.SaveAsync();

                return(new OperationDetails(true, "The registration was done successfully!", ""));
            }
            else
            {
                return(new OperationDetails(false, "The user with the same name already exists!", "Email"));
            }
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> Register(RegisterModel model)
        {
            await SetInitialDataAsync();

            if (ModelState.IsValid)
            {
                UserTransferModel userDto = new UserTransferModel
                {
                    Email    = model.Email,
                    Password = model.Password,
                    Address  = model.Address,
                    Name     = model.Name,
                    Role     = "user"
                };
                OperationDetails operationDetails = await _userService.Create(userDto);

                if (operationDetails.Succedeed)
                {
                    return(Ok("Register successful"));
                }
                else
                {
                    ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
                }
            }
            return(NotFound());
        }
Ejemplo n.º 3
0
        public async Task <IHttpActionResult> Login(LoginModel model)
        {
            await SetInitialDataAsync();

            if (ModelState.IsValid)
            {
                UserTransferModel userDto = new UserTransferModel {
                    Email = model.Email, Password = model.Password
                };
                ClaimsIdentity claim = await _userService.Authenticate(userDto);

                if (claim == null)
                {
                    ModelState.AddModelError("", "Wrong login or password!");
                }
                else
                {
                    AuthenticationManager.SignOut();
                    AuthenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, claim);
                    return(Ok(" successful!"));
                }
            }
            return(NotFound());
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Register(RegisterViewModel viewModel)
        {
            await SetInitialDataAsync();

            if (ModelState.IsValid)
            {
                var userDto = new UserTransferModel
                {
                    Email    = viewModel.Email,
                    Password = viewModel.Password,
                    Address  = viewModel.Address,
                    Name     = viewModel.Name,
                    Role     = "user"
                };
                OperationDetails operationDetails = await _userService.Create(userDto);

                if (operationDetails.Succedeed)
                {
                    return(View("SuccessRegister"));
                }
                else
                {
                    ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
                }
            }
            return(View(viewModel));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> Login(LoginViewModel viewModel)
        {
            await SetInitialDataAsync();

            if (ModelState.IsValid)
            {
                var userDto = new UserTransferModel {
                    Email = viewModel.Email, Password = viewModel.Password
                };
                ClaimsIdentity claim = await _userService.Authenticate(userDto);

                if (claim == null)
                {
                    ModelState.AddModelError("", "Invalid login or password.");
                }
                else
                {
                    AuthenticationManager.SignOut();
                    AuthenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, claim);
                    return(RedirectToAction("Index", "Home"));
                }
            }
            return(View(viewModel));
        }
Ejemplo n.º 6
0
        public async Task <ClaimsIdentity> Authenticate(UserTransferModel userDto)
        {
            ClaimsIdentity claim = null;
            // finding the user
            UserSecurity user = await Database.UserSecurityManager.FindAsync(userDto.Email, userDto.Password);

            // authorize and return the ClaimsIdentity object
            if (user != null)
            {
                claim = await Database.UserSecurityManager.CreateIdentityAsync(user,
                                                                               DefaultAuthenticationTypes.ApplicationCookie);
            }
            return(claim);
        }
Ejemplo n.º 7
0
        // initial data
        public async Task SetInitialData(UserTransferModel adminDto, List <string> roles)
        {
            foreach (string roleName in roles)
            {
                var role = await Database.RoleManager.FindByNameAsync(roleName);

                if (role == null)
                {
                    role = new ApplicationRole {
                        Name = roleName
                    };
                    await Database.RoleManager.CreateAsync(role);
                }
            }
            await Create(adminDto);
        }
Ejemplo n.º 8
0
        public void SaveUserProfile(UserProfileTransferModel userProfileDto)
        {
            // Validation
            UserProfileValidation(userProfileDto, false);

            //try to create user in the Identity DB
            UserTransferModel userDto = Mapper.Map <UserProfileTransferModel, UserTransferModel>(userProfileDto);

            userDto.Role = "user";
            OperationDetails identityResult = userService.Create(userDto).Result;

            if (identityResult.Succedeed)
            {
                var userProfile = Mapper.Map <UserProfileTransferModel, UserProfile>(userProfileDto);
                Database.UserProfiles.Create(userProfile);
                Database.Save();
            }
            else
            {
                throw new ValidationException(identityResult.Message, userProfileDto.Email); //return message from Create() method of UserService
            }
        }