简体   繁体   中英

MVC project uses Web API project inside same Docker container

I am trying to learn more about Docker in my free time. I am currently trying to get ASP.NET Identity Server to work with it.

Here are the steps I have taken:

  1. Download: https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/6_AspNetIdentity

  2. Run the ASP.NET MVC client application and the server application and browse to the following URL to make sure it is working properly: http://localhost:5002/Home/CallApiUsingClientCredentials

    The code looks like this:

     public async Task<IActionResult> CallApiUsingClientCredentials() { var tokenClient = new TokenClient("http://localhost:5000/connect/token", "mvc", "secret"); var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1"); var client = new HttpClient(); client.SetBearerToken(tokenResponse.AccessToken); var content = await client.GetStringAsync("http://localhost:5001/identity"); ViewBag.Json = JArray.Parse(content).ToString(); return View("Json"); } 

    I get the response I am expecting.

  3. Right click on the API project and select Add Container Orchestration Support.

  4. Right click on MVCClient and select: Add/Container Orchestration Support.

Docker Compose Override then looks like this:

version: '3.4'

services:
  mvcclient:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "5002:80"

  api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "5001:80"


  identityserverwithaspnetidentity:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - "5000:80"

Make changes to the code above as follows:

Line 3 change to:

var tokenClient = new TokenClient("http://identityserverwithaspnetidentity:80/connect/token", "mvc", "secret");

Line 10 change to:

var content = await client.GetStringAsync("http://api:80/Identity");

When I browse to http://localhost:5002/Home/CallApiUsingClientCredentials ; I am receiving a weird response:

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html><head><meta http-equiv=\"refresh\" content=\"0;url=http://www.webaddresshelp.bt.com/main?ParticipantID=mg76cjr54t8kx45jjw4j4k9j5hsr5m26&FailedURI=http%3A%2F%2Fapi1%2FIdentity&FailureMode=1&Implementation=&AddInType=4&Version=pywr1.0&ClientLocation=uk\"/><script type=\"text/javascript\">url=\"http://www.webaddresshelp.bt.com/main?ParticipantID=mg76cjr54t8kx45jjw4j4k9j5hsr5m26&FailedURI=http%3A%2F%2Fapi1%2FIdentity&FailureMode=1&Implementation=&AddInType=4&Version=pywr1.0&ClientLocation=uk\";if(top.location!=location){var w=window,d=document,e=d.documentElement,b=d.body,x=w.innerWidth||e.clientWidth||b.clientWidth,y=w.innerHeight||e.clientHeight||b.clientHeight;url+=\"&w=\"+x+\"&h=\"+y;}window.location.replace(url);</script></head><body></body></html>

What is the problem please?

Update

Why do the lines below return the output above (what I referred to as the weird response):

var content = await client.GetStringAsync("http://bert:80/Identity");
var content = await client.GetStringAsync("http://somerandomserver:80/Identity");
var content = await client.GetStringAsync("http://hello:80/Identity");

bert; somerandomserver and hello do not exist. It appears the response above is always received as long as I use port 80 ie regardless of the server name I use ie I can make it up. Why?

For web apps in docker compose , they are sharing the same networks in the Compose .

Per to your steps, it seems you only changed the request URLs. You also need to change the Authority in api project.

Try to change src\\Api\\Startup.cs with below:

            services.AddAuthentication("Bearer")
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority = "http://identityserverwithaspnetidentity:80";
                options.RequireHttpsMetadata = false;

                options.ApiName = "api1";
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM