Beispiel #1
0
 private async Task SeedUsers()
 {
     var standardPassword = seederData.DefaultPassword;
     var standardSalt     = seederData.DefaultSaltGuid.ToByteArray();
     var standardHash     = CoffeeHasher.HashWithSalt(standardPassword, standardSalt);
     var user1            = new User
     {
         Id              = Guid.Parse("{C2D94925-9587-4132-8E63-B69D80315FC3}"),
         Email           = "*****@*****.**",
         Name            = "Coffee Drinker",
         DeliveryAddress = "Alameda Barão de Limeira, 539",
         PasswordHash    = standardHash,
         PasswordSalt    = standardSalt,
         // Metadata
         CreatedAt = DateTime.Now,
     };
     // Adding to database context
     await db.Users.AddRangeAsync(user1);
 }
Beispiel #2
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                // Verify if there's any user with the same e-mail
                var userSameEmail = await db.Users
                                    .Where(x => x.DeletedAt == null)
                                    .FirstOrDefaultAsync(x => x.Email.Equals(request.Email, StringComparison.InvariantCultureIgnoreCase));

                if (userSameEmail != null)
                {
                    throw new ConflictException(new ErrorInformation {
                        Message = "User with same e-mail already exists"
                    });
                }
                // Create password information
                var salt = Guid.NewGuid().ToByteArray();
                var hash = CoffeeHasher.HashWithSalt(request.Password, salt);
                // Create the user
                var user = new User
                {
                    Id           = Guid.NewGuid(),
                    Name         = request.Name,
                    Email        = request.Email,
                    PasswordSalt = salt,
                    PasswordHash = hash,
                    // Metadata
                    CreatedAt = DateTime.Now,
                };
                // Save the user to database
                await db.Users.AddAsync(user);

                await db.SaveChangesAsync();

                // Return result
                return(new Result {
                    Id = user.Id
                });
            }