我有一个非常基本的自托管 .NET core 2.1 应用程序,具有以下配置:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
非常典型的简单 Controller 如下:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
当我测试它并导航到我的 HTTPS 本地端点端口(在我的例子中为 44325)时,该应用程序运行得很好:
https://localhost:44325/api/values
到目前为止一切都很好。现在我想弄清楚此 HTTPS 连接的证书来自哪里,因为我没有使用 IIS Express 并且该证书实际上不属于 IIS Express:
当我搜索其指纹时,我在证书存储中找不到上述证书。这个证书是如何生成的呢?我在哪里可以找到它?为什么此证书在 Edge 和 Chrome 中有效,但在 Firefox 中不受信任?它是动态生成的吗?
我的启动设置如下:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:55894",
"sslPort": 44325
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Experimental1": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:44325;http://localhost:55894",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
我使用 Experimental1 配置文件而不是 IIS Express,当我运行应用程序时,我看到了我的小控制台。
最佳答案
How does this certificate gets generated?
.NET Core SDK 在我们第一次运行 dotnet new
时生成证书
参见https://blogs.msdn.microsoft.com/webdev/2018/02/27/asp-net-core-2-1-https-improvements/
Where can I find it?
SDK 将 ASP.NET Core HTTPS 开发证书安装到本地用户证书存储中。
- Open the Certificates Snap-in in the MMC 。
- 证书 - 当前用户
- 查找证书
- 搜索
localhost
why does this certificate work in Edge and chrome but in Firefox its not trusted?
确实如此。即使在运行 dotnet dev-certs https --trust
后,Firefox 也不信任该证书,并提示“该证书不受信任,因为它是自签名的。”
可能只是 Firefox no longer trusts self-signed certificates 。我的解决方法是添加安全异常(exception)。
关于asp.net-core - 找不到 Kestrel 使用的自签名可信证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51267154/