예제 #1
0
    public async Task <IActionResult> GetWithNoAuthenticationNeeded()
    {
        var responseObject = await _requestServiceFactory
                             .CreateHttpClientService()                                  //Create a new instance of the HttpClientService
                             .GetAsync("http://localhost:5000/dummy-data/complex-type"); //Execute a GET request to any URL.

        if (!responseObject.HasError)                                                    //Check if there was an error in the process
        {
            return(Ok(responseObject.BodyAsString));                                     //If not, just return the body
        }
        else
        {
            return(StatusCode((int)responseObject.StatusCode, responseObject.Error));    //If an error is found, return the status code and the error description
        }
    }
        /// <summary>
        /// Sample request that returns a typed response using GET
        /// </summary>
        /// <returns>An <see cref="IEnumerable{ProtectedResourceResponseDto}"/>. </returns>
        public async Task <IEnumerable <ClientCredentialsProtectedResourceResponseDto> > GetProtectedResourceResults()
        {
            var response = await _requestServiceFactory
                           .CreateHttpClientService()
                           .SetIdentityServerOptions(_identityServerOptions)                                                                    //Set the options to retrieve an access token
                           .HeadersAdd("X-Request-Client", "IdentityServer4.Contrib.HttpClientService")                                         //Set custom headers for this request
                           .GetAsync <IEnumerable <ClientCredentialsProtectedResourceResponseDto> >("https://demo.identityserver.io/api/test"); //Execute the request

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return(response.BodyAsType);
            }
            else
            {
                //log, error handle, etc
                _logger.LogError(response.StatusCode + " " + response.Error);
                return(new List <ClientCredentialsProtectedResourceResponseDto>());
            }
        }
 /// <summary>
 /// Constructor of the <see cref="IntegrationTestsController"/>
 /// </summary>
 /// <param name="protectedResourceService"></param>
 public IntegrationTestsController(IHttpClientServiceFactory requestServiceFactory)
 {
     httpClientService = requestServiceFactory
                         .CreateHttpClientService()
                         .SetIdentityServerOptions("ProtectedResourceClientCredentialsOptions");
 }
 /// <summary>
 /// Constructor of the <see cref="IntegrationTestsController"/>
 /// </summary>
 /// <param name="protectedResourceService"></param>
 public IntegrationTestsController(IHttpClientServiceFactory requestServiceFactory)
 {
     httpClientService = requestServiceFactory
                         .CreateHttpClientService();
 }
 /// <summary>
 /// Constructor for the <see cref="TestApiService"/>.
 /// </summary>
 /// <param name="logger">The logger</param>
 /// <param name="requestServiceFactory">The <see cref="IHttpClientServiceFactory"/> that will perform the request to the protected resource.</param>
 /// <param name="identityServerOptions">The identity server options that will used to retrieve an access token.</param>
 public PasswordProtectedResourceService(ILogger <PasswordProtectedResourceService> logger, IHttpClientServiceFactory requestServiceFactory, IOptions <SomePasswordOptions> identityServerOptions)
 {
     _logger                = logger;
     _requestService        = requestServiceFactory.CreateHttpClientService();
     _identityServerOptions = identityServerOptions;
 }