Beispiel #1
0
        public async Task <IdentityResult> CreateAsync(EmployeeCreateDTO createModel)
        {
            if (createModel == null)
            {
                return(IdentityResult.Failed(new IdentityError
                {
                    Code = string.Empty,
                    Description = "Please fill in the required fields"
                }));
            }

            var user = new ApplicationUser
            {
                UserName       = createModel.Email,
                Email          = createModel.Email,
                Address        = "",
                FirstName      = createModel.FirstName,
                MiddleName     = createModel.MiddleName,
                LastName       = createModel.LastName,
                EmailConfirmed = true
            };

            IEnumerable <string> staffRoles = await StaffRoles();

            List <string> availableRoles = createModel.Roles?.Where(r =>
                                                                    staffRoles.Contains(r, StringComparer.OrdinalIgnoreCase)
                                                                    ).ToList();

            string         randomPwd = _pwdGenerator.Next();
            IdentityResult result    = await _userManager.CreateAsync(user, randomPwd);

            if (!result.Succeeded)
            {
                return(IdentityResult.Failed(result.Errors.ToArray()));
            }

            await _emailSender.SendEmailAsync(user.Email, "Register at the market",
                                              "Your employee account password: " + randomPwd);

            if (availableRoles == null || !availableRoles.Any())
            {
                return(result);
            }

            IdentityResult rolesResult = await _userManager
                                         .AddToRolesAsync(user, availableRoles);

            if (rolesResult.Succeeded)
            {
                return(result);
            }

            List <IdentityError> errors = result.Errors.ToList();

            errors.AddRange(rolesResult.Errors);
            result = IdentityResult.Failed(errors.ToArray());

            return(result);
        }
        public async Task RegisterAccount(int count)
        {
            using (var dbContext = new DbContext())
            {
                var accountService = new AccountDataService(dbContext);
                for (var i = 0; i < count; i++)
                {
                    using (var dungeonCrawlApi = _dungeonCrawlApiFactory.Factory())
                    {
                        dungeonCrawlApi.Proxy = new WebProxy(_proxyAddresses[i]);

                        var name = await _nameGenerator.GenerateAsync();

                        var emailDomain = _emailDomainGenerator.Next().First();
                        var password    = _passwordGenerator.Next().First();
                        var email       = $"{name}@{emailDomain}";

                        await dungeonCrawlApi.ConnectAsync(_serverLabel.Url);

                        await dungeonCrawlApi.RegisterAsync(name, password, email);

                        accountService.Add(new DataBase.Account
                        {
                            Email    = email,
                            Name     = name,
                            Password = password
                        });
                    }
                }
            }
        }