Example #1
0
        private IServiceProvider ConfigureServices()
        {
            var services = new ServiceCollection();

            var settings = new Settings();

            var publicClientApplication = PublicClientApplicationBuilder.Create(settings.ClientId).WithAuthority(settings.Authority).Build();

            var handler = new BearerTokenHttpClientHandler(publicClientApplication, settings);

            var httpClient = new HttpClient(handler)
            {
                BaseAddress = settings.BackendUrl
            };

            services.AddSingleton <ISettings>(settings);
            services.AddSingleton <IPublicClientApplication>(publicClientApplication);
            services.AddSingleton(httpClient);
            services.AddScoped <INavigation>(_ => new Navigation(Window.Current.Content as Frame));
            services.AddScoped <IActorRepository, ActorRepository>();
            services.AddScoped <ICharacterRepository, CharacterRepository>();
            services.AddScoped <IGraphRepository, GraphRepository>();
            services.AddScoped <MainViewModel>();
            services.AddScoped <CharactersViewModel>();
            services.AddScoped <CharacterViewModel>();
            services.AddScoped <MeViewModel>();

            return(services.BuildServiceProvider());
        }
Example #2
0
        /// <inheritdoc />
        public override async Task <HttpClient> GetAuthenticatedHttpClient()
        {
            // Initialize HTTP client
            if (_httpClient == null)
            {
                var handler = new BearerTokenHttpClientHandler(_bearerToken);

                _configureHandler?.Invoke(handler);

                _httpClient = new HttpClient(handler)
                {
                    BaseAddress = ServerUri
                };

                _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.HttpContentTypes.ApplicationJson));
            }

            // Authenticate?
            if (_authenticated)
            {
                return(_httpClient);
            }

            var response = await _httpClient.GetAsync("api/admin/users/me");

            if (response.IsSuccessStatusCode)
            {
                _authenticated = true;
            }
            else
            {
                var responseString = await response.Content.ReadAsStringAsync();

                throw new UnauthorizedConnectionException(
                          Strings.Exception_CouldNotAuthenticate, response.StatusCode, responseString);
            }

            return(_httpClient);
        }