Exemplo n.º 1
0
            public void SetUp()
            {
                _apiGatewayOptionsMock = new Mock <IOptions <ApiGatewayOptions> >();

                _apiGatewayOptionsMock.Setup(x => x.Value).Returns(new ApiGatewayOptions()
                {
                    BasePath = "http://apigateway"
                });

                _sut = new ApiGatewayHandler(_apiGatewayOptionsMock.Object)
                {
                    InnerHandler = new TestHandler()
                };
            }
Exemplo n.º 2
0
        private async Task <HttpClient> GetHttpClientAsync()
        {
            var cookieContainer = new CookieContainer();

#pragma warning disable CA2000 // Dispose objects before losing scope
            HttpMessageHandler handler = new SocketsHttpHandler
            {
                UseCookies              = true,
                AllowAutoRedirect       = false,
                CookieContainer         = cookieContainer,
                MaxConnectionsPerServer = 25
            };

            if (!string.IsNullOrEmpty(ProjectResource.ApiGatewayHost) && !string.IsNullOrEmpty(ProjectResource.ApiGatewayPolicy))
            {
                // since this is executed on every access to sharepoint, only log at debug level
                Logger.Debug("Using {@ApiGateway} for {Resource}",
                             new { Host = ProjectResource.ApiGatewayHost, Policy = ProjectResource.ApiGatewayPolicy },
                             ProjectResource.Resource);

                handler = new ApiGatewayHandler(handler, ProjectResource.ApiGatewayHost, ProjectResource.ApiGatewayPolicy);
            }
#pragma warning restore CA2000 // Dispose objects before losing scope

            HttpClient httpClient = new HttpClient(handler);
            httpClient.BaseAddress = ProjectResource.Resource;
            httpClient.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata=verbose"));

            // simplify the parameters
            var resource = ProjectResource.Resource;
            var relyingPartyIdentifier = ProjectResource.RelyingPartyIdentifier;
            var username         = ProjectResource.Username;
            var password         = ProjectResource.Password;
            var authorizationUrl = ProjectResource.AuthorizationUri.ToString();

            string samlToken = await _samlAuthenticator.GetStsSamlTokenAsync(relyingPartyIdentifier, username, password, authorizationUrl);

            string apiGatewayHost   = ProjectResource.ApiGatewayHost;
            string apiGatewayPolicy = ProjectResource.ApiGatewayPolicy;

            await _samlAuthenticator.GetSharepointFedAuthCookieAsync(resource, samlToken, httpClient, cookieContainer, apiGatewayHost, apiGatewayPolicy);

            return(httpClient);
        }