代码之家  ›  专栏  ›  技术社区  ›  w0051977

MVC项目在同一Docker容器中使用webapi项目

  •  1
  • w0051977  · 技术社区  · 6 年前

    我想在空闲时间多了解一下Docker。我目前正在尝试让ASP.NET Identity Server使用它。

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

    2. 运行ASP.NET MVC客户端应用程序和服务器应用程序,并浏览到以下URL以确保其正常工作: http://localhost:5002/Home/CallApiUsingClientCredentials

      代码如下所示:

      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");
      }
      

      我得到了我期待的回应。

    3. 右键单击MVCClient并选择:Add/Container Orchestration Support。

    Docker Compose Override如下所示:

    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"
    

    对上述代码进行如下更改:

    第3行改为:

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

    第10行改为:

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

    当我浏览到 http://localhost:5002/Home/CallApiUsingClientCredentials ; 我收到一个奇怪的答复:

    <!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>
    

    请问有什么问题?

    更新

    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");
    

    伯特;somerandomserver和hello不存在。似乎只要我使用端口80(即,不管我使用的服务器名称如何,即,我可以弥补),就会始终收到上面的响应。为什么?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Edward    6 年前

    中的web应用程序 docker compose Compose .

    Authority 在api项目中。

    试着改变 src\Api\Startup.cs 如下所示:

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