Ejemplo n.º 1
0
        public async Task <User> GetUserByFilterAsync(string filter)
        {
            var accessUri       = $"{_graphApiSettings.GraphApiBaseUriWindows}{_graphApiSettings.TenantId}/users?$filter={filter}&api-version=1.6";
            var responseMessage = await _secureHttpRequest.GetAsync(_graphApiSettings.AccessTokenWindows, accessUri);

            if (responseMessage.IsSuccessStatusCode)
            {
                var queryResponse = await responseMessage.Content
                                    .ReadAsAsync <AzureAdGraphQueryResponse <AzureAdGraphUserResponse> >();

                if (queryResponse.Value != null && queryResponse.Value.Any())
                {
                    var adUser = queryResponse.Value[0];
                    return(new User
                    {
                        Id = adUser.ObjectId,
                        DisplayName = adUser.DisplayName,
                        UserPrincipalName = adUser.UserPrincipalName,
                        GivenName = adUser.GivenName,
                        Surname = adUser.Surname,
                        Mail = adUser.OtherMails?.FirstOrDefault()
                    });
                }
                else
                {
                    return(null);
                }
            }

            if (responseMessage.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }

            var message = $"Failed to search user with filter {filter}";
            var reason  = await responseMessage.Content.ReadAsStringAsync();

            throw new UserServiceException(message, reason);
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <string> > GetUsernamesStartingWithAsync(string text)
        {
            var filterText = text.Replace("'", "''");
            var filter     = $"startswith(userPrincipalName,'{filterText}')";
            var queryUrl   = $"{_baseUrl}/users?$filter={filter}";
            var response   = await _secureHttpRequest.GetAsync(_graphApiSettings.AccessToken, queryUrl);

            await AssertResponseIsSuccessful(response);

            var result = await response.Content.ReadAsAsync <AzureAdGraphQueryResponse <User> >();

            return(result.Value.Select(user => user.UserPrincipalName));
        }