示例#1
0
        public static async Task SeedUsers(DataContext context)
        {
            if (await context.Users.AnyAsync())
            {
                return;
            }

            var userData = await System.IO.File.ReadAllTextAsync("Data/UserSeedData.json");

            var users = JsonSerializer.Deserialize <List <AppUser> >(userData);

            foreach (AppUser user in users)
            {
                using var hmac = new HMACSHA512();

                user.UserName     = user.UserName.ToLower();
                user.PasswordHash = await hmac.ComputeHashAsync(new MemoryStream(Encoding.UTF8.GetBytes("123456")));

                user.PasswordSalt = hmac.Key;

                await context.AddAsync(user);
            }

            await context.SaveChangesAsync();
        }
示例#2
0
        public async Task <IEnumerable <GetTestDto> > AddTestAsync(AddTestDto addTestDto)
        {
            Test test = _mapper.Map <Test>(addTestDto);
            await _context.AddAsync(test);

            await _context.SaveChangesAsync();

            List <Test> tests = await _context.Tests
                                .Include(t => t.Breakage)
                                .Include(t => t.Tags)
                                .ToListAsync();

            return(tests.Select(t => _mapper.Map <GetTestDto>(t)).ToList());
        }
示例#3
0
        public async Task <User> Register(User user, string password)
        {
            if (user == null || password == null)
            {
                return(null);
            }
            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            await _context.AddAsync(user);

            await _context.SaveChangesAsync();

            return(user);
        }
示例#4
0
        public async Task <bool> UploadImage(AppUser user)
        {
            foreach (var file in _httpContextAccessor.HttpContext.Request.Form.Files)
            {
                Image img = new Image();
                img.ImageTitle = file.FileName;

                MemoryStream ms = new MemoryStream();
                await file.CopyToAsync(ms);

                img.ImageData = ms.ToArray();

                string base64String = Convert.ToBase64String(img.ImageData);

                ms.Close();
                ms.Dispose();


                Photo photo = new Photo
                {
                    AppUserId = user.Id,
                    ImageData = img.ImageData,
                    Url       = file.FileName,
                    ImageType = file.ContentType,
                    isMain    = false,
                };

                if (photo.ImageData.Length > 0)
                {
                    await _context.AddAsync(photo);

                    await _context.SaveChangesAsync();

                    return(flag = true);
                }
                else
                {
                    return(flag = false);
                }
            }
            return(flag);
        }
示例#5
0
 public async Task Add <T>(T entity) where T : class
 {
     await _context.AddAsync(entity);
 }