Beispiel #1
0
        /// <summary>
        /// 创建新用户
        /// </summary>
        /// <param name="data">用户数据</param>
        /// <returns>新用户ID</returns>
        public async Task <int> CreateUserAsync([NotNull] PureUserData data)
        {
            UserCreateRequest request = new UserCreateRequest
            {
                Connection  = configuration.GetAuth0Config(ConfigurationKeyConstants.Auth0Connection),
                Email       = data.Email,
                PhoneNumber = data.PhoneNumber
            };

            User user = await userClient.CreateAsync(request);

            if (user == null)
            {
                return(-1);
            }

            int nextId = dataContext.UserDatas.Select((data) => data.ID).Max() + 1;

            // 获取可用身份列表
            Role[] availableRoles = await roleGetter.GetAvailableRoles();

            // 去除重复项与不在身份列表内的项
            string[] roles = data.Role.Split(' ').Distinct()
                             .Where((name) => availableRoles.Any((role) => role.Name == name)).ToArray();

            await userClient.AssignRolesAsync(user.UserId, new AssignRolesRequest { Roles = roles });

            data.Role = roles.Aggregate((r1, r2) => r1 + ' ' + r2);

            dataContext.Add <UserData>(data.ToUserData(nextId, user.UserId));
            dataContext.SaveChanges();
            return(nextId);
        }
Beispiel #2
0
        /*public async Task AddToRoleAsync(UserMetadata user, string roleName, CancellationToken cancellationToken)
         * {
         *  cancellationToken.ThrowIfCancellationRequested();
         *
         *  var httpclient = clientFactory.CreateClient();
         *  var client = new RolesClient(httpclient);
         *
         *  var role = await client.GetByNameAsync(roleName);
         *  var index = user.Roles.ToList().FindIndex(x => x.Id == role.Id);
         *  if (index != -1) return;
         *  user.Roles.Add(role);
         * }
         *
         *  public Task<IList<string>> GetRolesAsync(UserMetadata user, CancellationToken cancellationToken)
         * {
         *  cancellationToken.ThrowIfCancellationRequested();
         *
         *  if (user.Roles == null)
         *      return Task.FromResult<IList<string>>(Array.Empty<string>());
         *  return Task.FromResult<IList<string>>(user.Roles.Select(x => x.Name).ToList());
         * }
         * public Task<bool> IsInRoleAsync(UserMetadata user, string roleName, CancellationToken cancellationToken)
         * {
         *  cancellationToken.ThrowIfCancellationRequested();
         *
         *  var httpclient = clientFactory.CreateClient();
         *  var client = new UsersClient(httpclient);
         *  if (user.Roles == null)
         *      return Task.FromResult(false);
         *  return Task.FromResult(user.Roles.Any(x => x.NormalizedName == roleName));
         * }
         *
         * public Task RemoveFromRoleAsync(UserMetadata user, string roleName, CancellationToken cancellationToken)
         * {
         *  cancellationToken.ThrowIfCancellationRequested();
         *
         *  var rs = user.Roles.ToList();
         *  var index = rs.FindIndex(x => x.NormalizedName == roleName);
         *  if (index == -1) return Task.CompletedTask;
         *
         *  user.Roles.RemoveAt(index);
         *  return Task.CompletedTask;
         * }
         *
         * public Task<IList<UserMetadata>> GetUsersInRoleAsync(string roleName, CancellationToken cancellationToken)
         * {
         *  throw new NotImplementedException();
         * }*/

        public async Task <IdentityResult> CreateAsync(UserMetadata user, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            HttpClient  httpclient = clientFactory.CreateClient();
            UsersClient client     = new UsersClient(httpclient);

            try
            {
                await client.CreateAsync(user);

                return(IdentityResult.Success);
            }
            catch
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = $"Creating user {user.Name} failed."
                }));
            }
        }