首页 > 解决方案 > 无法连接到 IdentityServer4 快速入门中的 IdentityController

问题描述

当我在控制台客户端中执行这行代码时,我收到一条“主动拒绝”消息。(请注意,发现有效并且定向到端口 60540)。

  var c = new HttpClient();
  c.SetBearerToken(tr.AccessToken);
  var rsp = await c.GetAsync("http://localhost:5000/identity");

下面是我的launchSettings.json

  {
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:60540",
      "sslPort": 44386
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "commandLineArgs": "debug=true",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Service": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:5000"
    }
  }
}

下面是 Startup.cs 的相关代码

public void ConfigureServices (IServiceCollection services)
{
  IIdentityServerBuilder                    isb;

  services.AddControllers();
  services.AddAuthentication("Bearer").AddJwtBearer("Bearer", options =>
  {
    options.Authority = "http://localhost:5000";
    options.RequireHttpsMetadata = false;
    options.Audience = "api1";
  });
  isb = services.AddIdentityServer();
  isb.AddInMemoryIdentityResources(new List<IdentityResource>
  {
    new IdentityResources.OpenId(),
    new IdentityResources.Profile()
  }).
  AddInMemoryApiResources(new List<ApiResource>()
  {
    new ApiResource("api1", "My Api")
  }).
  AddInMemoryClients(new List<Client>()
  {
    new Client
    {
      ClientId = "client",
      AllowedGrantTypes = GrantTypes.ClientCredentials,
      ClientSecrets =
      {
        new Secret("secret".Sha256())
      },
      AllowedScopes =
      {
        "api1"
      }
    }
  });
  // not recommended for production - you need to store your key material somewhere secure
  isb.AddDeveloperSigningCredential();
}   /* method Startup ConfigureServices */

public void Configure (IApplicationBuilder app, IWebHostEnvironment env)
{
  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();
  }
  app.UseStaticFiles();
  app.UseRouting();
  app.UseIdentityServer();
  app.UseAuthentication();
  app.UseAuthorization();
  app.UseEndpoints(endpoints =>
  {
    endpoints.MapControllers();
  });
}   /* method Startup Configure */

我试过 IIS Express 配置文件和服务配置文件,结果是一样的。我将这个项目作为 aspnet core 3.1 的 VS 2019 Worker Service 项目开始

标签: asp.net-coreidentityserver4asp.net-core-3.1

解决方案


既然您说发现工作在端口 60540 上,您是否尝试过访问 http://localhost:60540/identity


推荐阅读