Exemple #1
0
        public virtual async Task AddToRoleAsync(User user, string normalizedRoleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            Throw.IfArgumentNull(user, nameof(user));
            Throw.IfArgumentNull(normalizedRoleName, nameof(normalizedRoleName));

            if (await IsInRoleAsync(user, normalizedRoleName, cancellationToken))
            {
                return;
            }

            var role = await _roleProvider.FindByNormalizedRoleNameAsync(normalizedRoleName);

            if (role == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Role {0} does not exist!", normalizedRoleName));
            }

            user.Roles.Add(new UserRole(user.Id, role.Id));
        }