コード例 #1
0
        public async Task <ResponseModel <List <string> > > CreateAccountByPhoneNumber(string phoneNumber)
        {
            var signUpModel = new SignUpModel
            {
                PhoneNumber = phoneNumber,
                UserName    = new Guid().ToString()
            };
            var result = await _userRepository.CreateAsync(MapApplicationUser.MapFromSignUpModel(signUpModel));

            if (result.Count == 0)
            {
                return(new GetResponse <List <string> >(null).GetSuccessResponse());
            }
            return(new GetResponse <List <string> >(result).GetSuccessResponse());
        }
コード例 #2
0
        public async Task <AuthorizationResponseModel> RegisterAsync(AuthorizationRequestModel registrationModel)
        {
            RoleData role = applicationRoleRepository.Get(registrationModel.Role);
            RegistrationResponseModel response = new RegistrationResponseModel()
            {
                IsSuccessful = false, Message = string.Empty
            };
            var userData = new UserData
            {
                Email    = registrationModel.Email,
                Password = registrationModel.Password,
                RoleId   = role.Id
            };

            IdentityResult userCreatingResult = await applicationUserRepository.CreateAsync(userData);

            if (!userCreatingResult.Succeeded)
            {
                // pushing message of first error in array
                response.Message = GetErrorMessage(userCreatingResult);
                return(response);
            }

            userData = await applicationUserRepository.FindByEmailAsync(userData.Email);

            ClientData client = new ClientData()
            {
                Name      = registrationModel.UserName,
                Surname   = registrationModel.Surname,
                PhotoPath = "default/profile.png",
                UserId    = userData.Id
            };
            ClientData addedClient = await clientRepository.AddAsync(client);

            if (addedClient == null)
            {
                response.Message = "Client not added";
            }
            response.IsSuccessful = true;
            string token       = javascriptWebTokenFactory.Create(userData.Id);
            var    sessionData = new SessionData
            {
                UserId = userData.Id,
                Token  = token,
            };
            await sessionRepository.CreateAsync(sessionData);

            response.Token = token;
            return(response);
        }
コード例 #3
0
        public async Task Save_Valid_User()
        {
            try
            {
                ApplicationUser applicationUser = new ApplicationUser
                {
                    UserName    = "******",
                    Email       = "*****@*****.**",
                    PhoneNumber = "3167040706"
                };

                IdentityResult result =
                    await _applicationUserRepository.CreateAsync(applicationUser, "ThisisaSecurePassword321*");

                Assert.IsTrue(result.Succeeded);

                SavedUserId = applicationUser.Id;
            }
            catch (DbUpdateException e)
            {
                Assert.Fail(e.Message);
            }
        }
コード例 #4
0
ファイル: EmployeesController.cs プロジェクト: cantte/Kaizen
        public async Task <ActionResult <EmployeeViewModel> > PostEmployee(EmployeeInputModel employeeModel)
        {
            EmployeeCharge employeeCharge = await _employeesRepository.GetAllEmployeeCharges()
                                            .FirstOrDefaultAsync(c => c.Id == employeeModel.ChargeId);

            if (employeeCharge is null)
            {
                return(BadRequest("El cargo del empleado no se encuentra registrado."));
            }

            Employee employee = _mapper.Map <Employee>(employeeModel);

            employee.EmployeeCharge = employeeCharge;

            IdentityResult result =
                await _applicationUserRepository.CreateAsync(employee.User, employeeModel.User.Password);

            if (!result.Succeeded)
            {
                return(this.IdentityResultErrors(result));
            }

            IdentityResult roleResult =
                await _applicationUserRepository.AddToRoleAsync(employee.User, GetEmployeeRole(employee));

            if (!roleResult.Succeeded)
            {
                return(this.IdentityResultErrors(roleResult));
            }

            _employeesRepository.Insert(employee);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateException)
            {
                if (EmployeeExists(employee.Id))
                {
                    return(Conflict($"Ya existe un empleado registrado con el código {employeeModel.Id}"));
                }

                throw;
            }

            return(_mapper.Map <EmployeeViewModel>(employee));
        }
コード例 #5
0
        public async Task <IActionResult> Register([FromBody] Usermodel username)
        {
            try
            {
                var login = await ApplicationUserRepository.CreateAsync(username);

                return(Ok(new LoginView {
                    IsSuccess = login.identityResult.Succeeded, User = new ApplicationUser {
                        Id = login.userId
                    }, Role = login.role, Token = login.Token.Token, Errors = login.identityResult.Errors.Select(error => error.Description).ToList()
                }));
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #6
0
        public async Task <IdentityResult> CreateAsync(ApplicationUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            int id = await applicationUserRepository.CreateAsync(user);

            if (id > 0)
            {
                user.Id = id;
                return(IdentityResult.Success);
            }

            return(IdentityResult.Failed(new IdentityError
            {
                Description = $"Could not insert user {user.UserName}"
            }));
        }
コード例 #7
0
ファイル: ClientsController.cs プロジェクト: cantte/Kaizen
        public async Task <ActionResult <ClientViewModel> > PostClient(ClientInputModel clientInput)
        {
            Client client = _mapper.Map <Client>(clientInput);

            IdentityResult result =
                await _applicationUserRepository.CreateAsync(client.User, clientInput.User.Password);

            if (!result.Succeeded)
            {
                return(this.IdentityResultErrors(result));
            }

            IdentityResult roleResult = await _applicationUserRepository.AddToRoleAsync(client.User, "Client");

            if (!roleResult.Succeeded)
            {
                return(this.IdentityResultErrors(roleResult));
            }

            client.PublishEvent(new SavedPerson(client));
            _clientsRepository.Insert(client);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateException)
            {
                if (ClientExists(clientInput.Id))
                {
                    return(Conflict($"El cliente con identificación {clientInput.Id} ya se encuentra registrado."));
                }

                throw;
            }

            return(_mapper.Map <ClientViewModel>(client));
        }
コード例 #8
0
 public Task CreateAsync(ApplicationUser user)
 {
     return(_userRepository.CreateAsync(user));
 }