对于 ASP.Net MVC 或 Web 表单,具有某些路由配置,您最终会将此 URL 视为路由引擎传递给 MVC/Forms 处理程序的东西,而不是静态文件返回。结果将是 404 或 503。解决方案非常简单:
如果您还没有,请放置挑战文件:
- 创建必要的目录 -
.well-known
很棘手,主要是因为 Microsoft 很懒惰,但您可以从 cmdline 执行此操作,也可以创建文件夹,因为.well-known.
Windows资源管理器会注意到解决方法并为您删除尾随句点。
- 在里面
\.well-known\acme-challenge
放置具有正确名称和内容的挑战文件。你可以随心所欲地进行这部分;我碰巧像使用 Git Bashecho "oo0acontents" > abcdefilename
然后使用以下内容在 acme-challenge 目录中创建一个 Web.Config 文件:
<?xml version = "1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clear />
<mimeMap fileExtension = ".*" mimeType="text/json" />
</staticContent>
<handlers>
<clear />
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule"
resourceType="Either" requireAccess="Read" />
</handlers>
</system.webServer>
</configuration>
来源:https ://github.com/Lone-Coder/letsencrypt-win-simple/issues/37
完毕。该文件将开始返回,而不是 404/503 允许挑战完成 - 您现在可以提交并验证您的域。
另外:上面的代码片段将 content-type 设置为 json,这是一个与 letencrypt 不再相关的历史要求。当前的要求是没有要求 - 您可以发送无裤/大象的内容类型,它仍然可以工作。
更多关于 Asp.Net
我喜欢将所有 HTTP 请求重定向回 HTTPS,以确保即使用户不知道要询问,他们最终也能建立安全连接。有很多简单的方法可以做到这一点,直到您使用 LetsEncrypt - 因为您将中断对 .well-known 的请求。您可以在类中设置静态方法,如下所示:
public static class HttpsHelper
{
public static bool AppLevelUseHttps =
#if DEBUG
false;
#else
true;
#endif
public static bool Application_BeginRequest(HttpRequest Request, HttpResponse Response)
{
if (!AppLevelUseHttps)
return false;
switch (Request.Url.Scheme)
{
case "https":
return false;
#if !DEBUG
case "http":
var reqUrl = Request.Url;
var pathAndQuery = reqUrl.PathAndQuery;
// Let's Encrypt exception
if (pathAndQuery.StartsWith("/.well-known"))
return false;
// http://stackoverflow.com/a/21226409/176877
var url = "https://" + reqUrl.Host + pathAndQuery;
Response.Redirect(url, true);
return true;
#endif
}
return false;
}
}
现在,除了 LetsEncrypt 敲门时,它可以很好地重定向到 HTTPS。在 Global.asax.cs 中绑定它:
protected void Application_BeginRequest(object sender, EventArgs ev)
{
HttpsHelper.Application_BeginRequest(Request, Response);
}
请注意,返回的布尔值在此处被丢弃。如果你想决定是否立即结束请求/响应,你可以使用它,真正的意思,结束它。
最后,如果您愿意,可以使用 AppLevelUseHttps 变量在需要时关闭此行为,例如测试在没有 HTTPS 的情况下是否可以正常工作。例如,您可以将其设置为 Web.Config 变量的值。