Пример #1
0
        public async Task <IdentityResult> UpdateRolesAsync(User user, List <string> roles)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (roles == null)
            {
                throw new ArgumentNullException(nameof(roles));
            }
            // 移除旧的角色数据
            var oldRoles = await GetRolesAsync(user);

            if (oldRoles != null)
            {
                foreach (var role in oldRoles)
                {
                    await _userStore.RemoveFromRoleAsync(user, role, CancellationToken);
                }
            }
            // 增加指定的角色数据
            foreach (var role in roles)
            {
                await _userStore.AddToRoleAsync(user, role, CancellationToken);
            }
            return(await UpdateUserAsync(user));
        }