alaa_sayegh
alaa_sayegh

Reputation: 2211

Https required for asp.net core in docker

I have an ASP.Net Core application which I deployed to docker. This ASP.Net app acts as identity server 4 to deliver client tokens. Besides, I have a simple c# client (console .net core 2.1), that makes a discovery request to this ASP.Net app and collects metadata, for example endpoints.

Locally everything is working fine, either the identity server or the client are running and i'm getting response in the client from the identity server api.

I deployed the identity server api to docker and ran it. It worked fine and i'm able to browse the index page, as below image:

enter image description here But when i try to run the client to request a token from the identity server api (hosted on docker), it throws an exception (Https is required) - See below image:

enter image description here

How can I bypass this HTTPs requirement? Or do I need to create a certificate locally? Is there any documentation which guides me on how I do achieve this?

Edit:

If I try to call over https, then it gives the following error:

enter image description here

which makes actually sense, as we didn't configure the ssl port.

Another point:

After I execute the docker run command to run the app in container and expose the port 5000, I only get the info that the container is now listening on port 80 (http). Where actually both should be configured (http & https), right? As far I know (i'm not that expert in this). See image:

enter image description here

Any help is highly appreciated.

Upvotes: 1

Views: 824

Answers (2)

hwj383
hwj383

Reputation: 11

  var disco = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest()
            {
                Address = tokenUrl,
                Policy = { RequireHttps = false }
            });

Upvotes: 1

alaa_sayegh
alaa_sayegh

Reputation: 2211

I was able to solve the problem myself. So if any body faces the same issue, you can find the solution here.

It was actually a problem with the DiscoveryClient class and the GetAsync method:

DiscoveryClient.GetAsync

I switched to a normal PostAsync request and it worked. Here my code:

Instead of calling this:

 var discoveryClient = await DiscoveryClient.GetAsync("http://url-or-machine-ip:portnumber");
 var token = new TokenClient(discoveryClient.TokenEndpoint, "my-client-id", "my-secret");
 var response = await token.RequestClientCredentialsAsync("my-api-name");

I wrote my own post request method:

private static string GetApiToken()
{
   using (var client = new HttpClient())
   {
     //setup client
     client.BaseAddress = new Uri("http://url-or-machine-ip:portnumber");
     client.DefaultRequestHeaders.Accept.Clear();
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

     //setup login data
     var formContent = new FormUrlEncodedContent(new[]
     {
        new KeyValuePair<string, string>("grant_type", "client_credentials"),
        new KeyValuePair<string, string>("client_id", "my-client-id"),
        new KeyValuePair<string, string>("client_secret", "my-secret"),
        new KeyValuePair<string, string>("scope", "my-api-name")
     });

     //send request
     var responseMessage = client.PostAsync("connect/token", formContent).Result;

     if (responseMessage.IsSuccessStatusCode)
     {
        //get access token from response body
        var responseJson = responseMessage.Content.ReadAsStringAsync().Result;
        var jObject = JObject.Parse(responseJson);
        return jObject.GetValue("access_token").ToString();
      }

      return string.Empty;
   }
 }

and in the client, just called:

var token = GetApiToken();

and i got my token from the identity server hosted/deployed to docker container:

enter image description here

Upvotes: 2

Related Questions