Exemplo n.º 1
0
        private async static Task CreateRootUser(IWebHost host)
        {
            using (IServiceScope scope = host.Services.CreateScope())
            {
                IStudentManagmentService studMngService =
                    scope.ServiceProvider.GetRequiredService <IStudentManagmentService>();
                IConfiguration  config  = host.Services.GetRequiredService <IConfiguration>();
                RegisterRootDTO rootDto = new RegisterRootDTO();
                config.GetSection("RootUser").Bind(rootDto);

                if (!await studMngService.IsStudentRegistered(rootDto.Email))
                {
                    await studMngService.RegisterRootUser(rootDto);
                }
            }
        }
        public async Task RegisterRootUser(RegisterRootDTO rootData)
        {
            StudentDTO studentDTO = Mapping.Mapper.Map <StudentDTO>(rootData);

            byte[] salt       = Security.GenerateRandomBytes(Constants.SALT_SIZE);
            string saltBase64 = Convert.ToBase64String(salt);
            string hash       = Security.CreateHash(rootData.Password, salt,
                                                    Constants.PASSWORD_HASH_SIZE);

            studentDTO.PasswordHash = hash;
            studentDTO.Salt         = saltBase64;
            studentDTO.Privilege    = StudentPrivilege.SeniorAdmin;
            studentDTO.Rating       = 0;

            ResultMessage <StudentDTO> studentResult = await _studentService.Add(studentDTO);

            if (!studentResult.IsSuccess)
            {
                throw new CouldNotRegisterRootUserException(studentResult.Status.ToString());
            }
        }