コード例 #1
0
        public async Task RegisterAdminAsync(ApplicationUser applicationUser)
        {
            await applicationUserRepository.AddAsync(applicationUser);

            await applicationUserRepository.SaveChangesAsync();

            string password = passwordGenerator.GeneratePassword();

            await AddUserPasswordAsync(applicationUser.User, password);
            await AddUserToRoleAsync(applicationUser.User, Roles.ADMIN);

            await emailService.SendEmailAsync(applicationUser.User.Email, password);
        }
コード例 #2
0
        public async Task AddAsync(RegisterModel model)
        {
            var cnpjHelper = new CNPJHelper();

            await Validate(model.CNPJ, cnpjHelper);

            model.CNPJ = cnpjHelper.Clean(model.CNPJ);

            if (await _applicationUserRepository.AnyAsync(x => x.Email.Equals(model.Email)))
            {
                throw new CustomExceptions("Este email já está cadatrado");
            }

            using (_unitOfWork)
            {
                var id   = Guid.NewGuid();
                var user = new ApplicationUser
                {
                    Email             = model.Email,
                    UserName          = model.Email,
                    CreatedDate       = DateTimeOffset.UtcNow,
                    ConcurrencyStamp  = Guid.NewGuid().ToString(),
                    EmailConfirmed    = true,
                    SecurityStamp     = Guid.NewGuid().ToString(),
                    AccessFailedCount = 0,
                    ID = id,
                    Id = id.ToString(),
                    NormalizedUserName = model.Email.ToUpper(),
                    NormalizedEmail    = model.Email.ToUpper(),
                };

                var hasher = new PasswordHasher <ApplicationUser>().HashPassword(user, model.Password);
                user.PasswordHash = hasher;

                await _applicationUserRepository.AddAsync(user);

                var company = await _companyRepository.FirstOrDefaultAsync(x => x.CNPJ.Equals(model.CNPJ));

                await _userCompanyRepository.AddAsync(new UserCompany { CompanyID = company.ID, UserID = id.ToString() });

                await _userManager.AddToRoleAsync(user, "USER");

                await _unitOfWork.CommitAsync();
            }
        }