示例#1
0
        public static async Task <string> GetUserDescriptorAsync(this GraphHttpClient client, Guid originId, IMemoryCache cache = null)
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (cache != null && cache.TryGetValue <string>($"{nameof(GetUserDescriptorAsync)}|{originId}", out var descriptor))
            {
                return(descriptor);
            }

            var page = await client
                       .ListUsersAsync()
                       .ConfigureAwait(false);

            await foreach (var user in page.AsContinousEnumerationAsync((continuationToken) => client.ListUsersAsync(continuationToken: continuationToken)))
            {
                cache?.Set($"{nameof(GetUserDescriptorAsync)}|{user.OriginId}", user.Descriptor, new MemoryCacheEntryOptions()
                {
                    SlidingExpiration = TimeSpan.FromMinutes(5)
                });

                if (originId.ToString().Equals(user.OriginId, StringComparison.OrdinalIgnoreCase))
                {
                    return(user.Descriptor);
                }
            }

            return(null);
        }
示例#2
0
        internal PagedGraphUsers GetAllUsers()
        {
            GraphHttpClient graphClient = connection.GetClient <GraphHttpClient>();
            PagedGraphUsers users       = graphClient.ListUsersAsync().Result;

            return(users);
        }
示例#3
0
        public async Task <List <string> > GetUsers(string organization)
        {
            VssConnection connection = null;

            try
            {
                string url = $"https://dev.azure.com/{organization}";
                if (_adoConfig.UsePta)
                {
                    connection = new VssConnection(new Uri(url), new VssBasicCredential(string.Empty, _adoConfig.AdoPersonalAccessToken));
                }
                //connection = new VssConnection()
                else
                {
                    connection = new VssConnection(new Uri(url), new VssClientCredentials(true));
                }

                GraphHttpClient  graphClient = connection.GetClient <GraphHttpClient>();
                PagedGraphGroups groups      = await graphClient.ListGroupsAsync();

                var users = await graphClient.ListUsersAsync();

                return(users?.GraphUsers.Where(a => a.Origin.Contains("ad", StringComparison.OrdinalIgnoreCase))?.Select(a => a.DisplayName)?.ToList());


                //UserHttpClient userHttpClient = connection.GetClient<UserHttpClient>();

                //TeamHttpClient teamHttpClient = connection.GetClient<TeamHttpClient>();

                //AccountHttpClient accountHttpClient = connection.GetClient<AccountHttpClient>();
                //var res = await accountHttpClient.GetAccountsAsync();
                //return res.Select(a => a.AccountName).ToList();

                //ProfileHttpClient profileHttpClient = connection.GetClient<ProfileHttpClient>();

                //OrganizationHttpClient organizatioClient = connection.GetClient<OrganizationHttpClient>();

                //var res = profileHttpClient.ge
                //var res = await teamHttpClient.GetTeamMembers()

                return(null);
            }
            catch (Exception ex)
            {
                //throw;
            }
            finally
            {
                connection?.Dispose();
            }
            return(null);
        }
    internal static async IAsyncEnumerable <GraphUser> ListAllUsersAsync(this GraphHttpClient client)
    {
        if (client is null)
        {
            throw new ArgumentNullException(nameof(client));
        }

        var graphUsersPage = await client
                             .ListUsersAsync()
                             .ConfigureAwait(false);

        await foreach (var graphUser in graphUsersPage.AsContinuousCollectionAsync((continuationToken) => client.ListUsersAsync(continuationToken: continuationToken)))
        {
            yield return(graphUser);
        }
    }
示例#5
0
        public PagedGraphUsers GetAllUsers()
        {
            VssConnection   connection  = Context.Connection;
            GraphHttpClient graphClient = connection.GetClient <GraphHttpClient>();
            PagedGraphUsers users       = graphClient.ListUsersAsync().Result;

            foreach (var user in users.GraphUsers)
            {
                Context.Log("{0} {1} {2}",
                            user.Descriptor.ToString().PadRight(8),
                            user.DisplayName.PadRight(20),
                            user.PrincipalName.PadRight(20)
                            );
            }

            return(users);
        }
示例#6
0
        internal static async Task <string> GetUserDescriptorAsync(this GraphHttpClient client, Guid originId, IDistributedCache cache = null)
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (cache != null)
            {
                var descriptor = await cache
                                 .GetStringAsync(GetCacheKey(originId.ToString()))
                                 .ConfigureAwait(false);

                if (!string.IsNullOrEmpty(descriptor))
                {
                    return(descriptor);
                }
            }

            var page = await client
                       .ListUsersAsync()
                       .ConfigureAwait(false);

            await foreach (var user in page.AsContinousEnumerationAsync((continuationToken) => client.ListUsersAsync(continuationToken: continuationToken)))
            {
                cache?.SetStringAsync(GetCacheKey(user.OriginId), user.Descriptor, new DistributedCacheEntryOptions()
                {
                    SlidingExpiration = TimeSpan.FromHours(1)
                });

                if (originId.ToString().Equals(user.OriginId, StringComparison.OrdinalIgnoreCase))
                {
                    return(user.Descriptor);
                }
            }

            throw new ArgumentOutOfRangeException(nameof(originId));

            string GetCacheKey(string identifier)
            => $"{nameof(GetUserDescriptorAsync)}|{originId}";
        }