示例#1
0
        private async Task UpdateRolesAsync(string userId, string[] roles, PasswordBoxUserManager manager = null)
        {
            if (manager == null)
            {
                manager = PasswordBoxUserManager.Create();
            }

            var user = await manager.FindByIdAsync(userId);

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var userRoles = await manager.GetRolesAsync(user);

            if (userRoles != null && userRoles.Count > 0)
            {
                await manager.RemoveFromRolesAsync(user, userRoles);
            }

            if (roles != null)
            {
                await manager.AddToRolesAsync(user, roles);
            }
        }
示例#2
0
        public async Task <User> GetByID(string id)
        {
            User result = null;

            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                result = await manager.FindByIdAsync(id);
            }

            return(result);
        }
示例#3
0
        public async Task <IActionResult> ConfirmEmail(string userId, string code)
        {
            if (userId == null || code == null)
            {
                return(RedirectToAction(nameof(HomeController.Index), "Home"));
            }
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{userId}'.");
            }
            var result = await _userManager.ConfirmEmailAsync(user, code);

            return(View(result.Succeeded ? "ConfirmEmail" : "Error"));
        }
示例#4
0
        public async Task DeleteAsync(string id)
        {
            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                var user = await manager.FindByIdAsync(id);

                if (user != null)
                {
                    var result = await manager.DeleteAsync(user);

                    if (!result.Succeeded)
                    {
                        throw new BusinessException(result.Errors.Select(a => a.Description).ToList());
                    }
                }
            }
        }
示例#5
0
        public async Task <User> UpdateAsync(User entity, string[] roles = null)
        {
            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                var user = await manager.FindByIdAsync(entity.Id);

                user = user.Update(entity);

                var result = await manager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    throw new BusinessException(result.Errors.Select(a => a.Description).ToList());
                }

                await UpdateRolesAsync(entity.Id, roles, manager);
            }


            return(entity);
        }
示例#6
0
        public async Task <List <string> > GetRolesByUserIdAsync(string id)
        {
            if (String.IsNullOrWhiteSpace(id))
            {
                return(new List <string>());
            }

            List <string> result = new List <string>();

            using (PasswordBoxUserManager manager = PasswordBoxUserManager.Create())
            {
                var user = await manager.FindByIdAsync(id);

                if (user != null)
                {
                    var roles = await manager.GetRolesAsync(user);

                    result = roles.ToList();
                }
            }

            return(result);
        }