Exemplo n.º 1
0
        public async Task PostDataInitializeAsync(IAppUserManager userManager)
        {
            var ghanbari = await userManager.FindByNameAsync("*****@*****.**");

            var bagherian = await userManager.FindByNameAsync("*****@*****.**");

            var admin = await userManager.FindByNameAsync("admin");

            await userManager.AddToRoleAsync(ghanbari.Id, PreDefRoles.Developer);

            await userManager.AddToRoleAsync(ghanbari.Id, PreDefRoles.Supervisor);

            await userManager.AddToRoleAsync(bagherian.Id, PreDefRoles.Developer);

            await userManager.AddToRoleAsync(bagherian.Id, PreDefRoles.Supervisor);

            await userManager.AddToRoleAsync(admin.Id, PreDefRoles.PrincipalAdministrator);
        }
        public async Task <LoginRegisterResponse> Handle(RegisterAsTeacherCommand request,
                                                         CancellationToken cancellationToken)
        {
            if (_userManager.Users.Any(x => x.UserName == request.TeacherOrStudentNo))
            {
                return(UserAlreadyExists());
            }
            if (_userManager.Users.Any(x => x.Email == request.EmailAddress))
            {
                return(EmailAlreadyExists());
            }
            var user = CreateUserModel(request);
            await _userManager.CreateAsync(user, request.Password);

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

            return(await SuccessWithJwtToken(user));
        }
Exemplo n.º 3
0
        private User CreateGeuestUserAndAddCookieToRequest()
        {
            var user = new User()
            {
                CreateDateTime = DateTime.Now,
                UpdateDateTime = DateTime.Now,
                Id             = Guid.NewGuid(),
                UserName       = Guid.NewGuid().ToString().Replace("-", ""),
            };

            var res = AsyncHelper.RunSync(() => _userManager.CreateAsync(user));

            if (!res.Succeeded)
            {
                throw new InvalidOperationException(string.Join(",", res.Errors));
            }

            AsyncHelper.RunSync(() => _userManager.AddToRoleAsync(user.Id, PreDefRoles.Guest));
            HttpContext.Current.Request.Cookies.Add(new HttpCookie(_guestUserCookieName)
            {
                Value = user.Id.ToString(), Expires = DateTime.Now.AddYears(1)
            });
            return(user);
        }
Exemplo n.º 4
0
        public async Task <(User, IdentityResult)> CreateAdminUserAsync()
        {
            var adminUser = await _userManager.FindByNameAsync(AdminUserSeedInfo.Username);

            if (adminUser != null)
            {
                _logger.LogInformation($"{AdminUserSeedInfo.Username} already existed.");
                return(adminUser,
                       IdentityResult.Success
                       );
            }

            var adminRole = await _roleManager.FindByNameAsync(AdminUserSeedInfo.RoleName);

            if (adminRole != null)
            {
                _logger.LogInformation($"{AdminUserSeedInfo.RoleName} role already existed.");
            }
            else
            {
                adminRole = new Role(AdminUserSeedInfo.RoleName);
                var roleResult = await _roleManager.CreateAsync(adminRole);

                if (roleResult == IdentityResult.Failed())
                {
                    _logger.LogError($"'{adminRole.Name}' role creating has failed. {roleResult.DumpErrors()}");
                    return(null,
                           IdentityResult.Failed()
                           );
                }
            }

            adminUser = new User {
                UserName       = AdminUserSeedInfo.Username,
                Email          = AdminUserSeedInfo.Email,
                EmailConfirmed = true,
                RegisterDate   = _dateSvc.UtcNow(),
                Title          = AdminUserSeedInfo.Title,
                LockoutEnabled = true,
                Status         = UserStatus.Enabled
            };

            var result = await _userManager.CreateAsync(adminUser, AdminUserSeedInfo.Password);

            if (result == IdentityResult.Failed())
            {
                _logger.LogError($"'{AdminUserSeedInfo.Username}; user creation has failed. {result.DumpErrors()}");
                return(null,
                       IdentityResult.Failed()
                       );
            }

            var setLockoutResult = await _userManager.SetLockoutEnabledAsync(adminUser, enabled : false);

            if (setLockoutResult == IdentityResult.Failed())
            {
                _logger.LogError($"'{AdminUserSeedInfo.Username}' setLockout on user has failed. {setLockoutResult.DumpErrors()}");
                return(null,
                       IdentityResult.Failed()
                       );
            }

            var addRoleResult = await _userManager.AddToRoleAsync(adminUser, AdminUserSeedInfo.RoleName);

            if (addRoleResult == IdentityResult.Failed())
            {
                _logger.LogError($"Adding user:'******' to role:'{AdminUserSeedInfo.RoleName}' has failed. {addRoleResult.DumpErrors()}");
                return(null,
                       IdentityResult.Failed()
                       );
            }

            return(
                adminUser,
                IdentityResult.Success
                );
        }