asp.net-core - 无法连接到 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 项目开始
解决方案
既然您说发现工作在端口 60540 上,您是否尝试过访问 http://localhost:60540/identity?
推荐阅读
- ios - 将 depthMap 缓冲区写入文件(Swift)
- android - Xamarin.Android 在页面之间导航时出现问题
- c++ - 迭代具有自定义开始/结束位置的容器
- sql-server - 优化查询,我的查询执行时间超过 5 分钟
- java - TelephonyManager.getSubscriberId 的权限 READ_PRIVILEGED_PHONE_STATE 不可用,
- android - Gitlab CI :- 如何在 Gitlab 中通过电子邮件共享工件(apks 或 build)?
- neo4j - Neo4j:匹配具有另外 2 个共同属性的节点
- java - 为什么我的连接已经关闭?
- excel - 基于其他列结构使用 VBA 控制(隐藏、显示)列
- node.js - 如何在量角器的循环中检查元素是否可点击?