将 https 与 IdentityServer4 快速入门一起使用挂起
Using https with IdentityServer4 quickstart hangs
我正在尝试学习 IdentityServer4,所以我从这里的文档开始 https://identityserver4.readthedocs.io/en/dev/quickstarts/0_overview.html and ended with this https://identityserver4.readthedocs.io/en/dev/quickstarts/1_client_credentials.html。
只要我使用 http,一切都很好,但一旦我切换到使用 https,客户端测试器中的第一行代码
var disco = await DiscoveryClient.GetAsync("https://localhost:44384");
挂起并最终获得任务取消异常。
如果我将所有内容都切换回 http,它会再次开始工作。
在实际的IdentityServer方案中,我在launchSettings.json中更改了applicationUrl和launchUrl:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://localhost:44384/",
"sslPort": 44384
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Mvcg.IdentityServer": {
"commandName": "Project",
"launchUrl": "https://localhost:44384",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
和 Program.cs
中的 .UseUrls
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("https://localhost:44384")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
在客户端解决方案中,如上所述,我更改了调用发现端点的代码以使用 https 而不是 http
var disco = await DiscoveryClient.GetAsync("https://localhost:44384");
需要说明的是,只需更改 http 和 https 之间的所有已注明项目对 http 有效,但对 https 无效。我的两次测试之间没有其他变化。
我是否遗漏了什么导致 https 无法正常工作?
看起来问题出在控制台托管上。只要我 运行 IIS Express 中的 IdentityServer 主机,一切正常。每次我尝试将其切换到控制台托管时,它都会失败。托管控制台中显示的错误是:
info: Microsoft.AspNetCore.Server.Kestrel[17]
Connection id "0HKVQ4SEO0GOI" bad request data: "The input string contains non-ASCII or null characters."
Microsoft.AspNetCore.Server.Kestrel.BadHttpRequestException: The input string contains non-ASCII or null characters.
at Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure.MemoryPoolIteratorExtensions.GetAsciiString(MemoryPoolIterator start, MemoryPoolIterator end)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.TakeStartLine(SocketInput input)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Connection processing ended abnormally
Microsoft.AspNetCore.Server.Kestrel.BadHttpRequestException: The input string contains non-ASCII or null characters.
at Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure.MemoryPoolIteratorExtensions.GetAsciiString(MemoryPoolIterator start, MemoryPoolIterator end)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.TakeStartLine(SocketInput input)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
不确定具体如何修复该错误,所以现在,我将不得不在 IIS Express 中托管以继续前进。
我正在尝试学习 IdentityServer4,所以我从这里的文档开始 https://identityserver4.readthedocs.io/en/dev/quickstarts/0_overview.html and ended with this https://identityserver4.readthedocs.io/en/dev/quickstarts/1_client_credentials.html。
只要我使用 http,一切都很好,但一旦我切换到使用 https,客户端测试器中的第一行代码
var disco = await DiscoveryClient.GetAsync("https://localhost:44384");
挂起并最终获得任务取消异常。
如果我将所有内容都切换回 http,它会再次开始工作。
在实际的IdentityServer方案中,我在launchSettings.json中更改了applicationUrl和launchUrl:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://localhost:44384/",
"sslPort": 44384
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Mvcg.IdentityServer": {
"commandName": "Project",
"launchUrl": "https://localhost:44384",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
和 Program.cs
中的 .UseUrlspublic static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("https://localhost:44384")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
在客户端解决方案中,如上所述,我更改了调用发现端点的代码以使用 https 而不是 http
var disco = await DiscoveryClient.GetAsync("https://localhost:44384");
需要说明的是,只需更改 http 和 https 之间的所有已注明项目对 http 有效,但对 https 无效。我的两次测试之间没有其他变化。
我是否遗漏了什么导致 https 无法正常工作?
看起来问题出在控制台托管上。只要我 运行 IIS Express 中的 IdentityServer 主机,一切正常。每次我尝试将其切换到控制台托管时,它都会失败。托管控制台中显示的错误是:
info: Microsoft.AspNetCore.Server.Kestrel[17]
Connection id "0HKVQ4SEO0GOI" bad request data: "The input string contains non-ASCII or null characters."
Microsoft.AspNetCore.Server.Kestrel.BadHttpRequestException: The input string contains non-ASCII or null characters.
at Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure.MemoryPoolIteratorExtensions.GetAsciiString(MemoryPoolIterator start, MemoryPoolIterator end)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.TakeStartLine(SocketInput input)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Connection processing ended abnormally
Microsoft.AspNetCore.Server.Kestrel.BadHttpRequestException: The input string contains non-ASCII or null characters.
at Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure.MemoryPoolIteratorExtensions.GetAsciiString(MemoryPoolIterator start, MemoryPoolIterator end)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame.TakeStartLine(SocketInput input)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.<RequestProcessingAsync>d__2.MoveNext()
不确定具体如何修复该错误,所以现在,我将不得不在 IIS Express 中托管以继续前进。