对于 ASP.Net MVC 或 Web 表单,具有某些路由配置,您最终会将此 URL 视为路由引擎传递给 MVC/Forms 处理程序的东西,而不是静态文件返回。结果将是 404 或 503。解决方案非常简单:
如果您还没有,请放置挑战文件:
- 创建必要的目录 -
.well-known 很棘手 mostly because Microsoft is lazy,但您可以从 cmdline 执行此操作,也可以将文件夹创建为 .well-known. 和 Windows Explorer will notice the workaround and remove the trailing period for you。
- 在
\.well-known\acme-challenge 中放置具有正确名称和内容的质询文件。你可以随心所欲地进行这部分;我碰巧像echo "oo0acontents" > abcdefilename一样使用Git Bash
然后在 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 以允许挑战完成 - 您现在可以提交并验证您的域。
旁白:上面的代码 sn-p 将 content-type 设置为 json,这是一个与 Letsencrypt 不再相关的历史要求。当前的要求是没有要求 - 您可以发送内容类型的裤子/大象,它仍然可以工作。
更多关于 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;
}
}
现在它可以很好地重定向到 HTTPS,除非 LetsEncrypt 来敲门。在 Global.asax.cs 中绑定它:
protected void Application_BeginRequest(object sender, EventArgs ev)
{
HttpsHelper.Application_BeginRequest(Request, Response);
}
请注意,返回的布尔值在此处被丢弃。如果你喜欢决定是否立即结束请求/响应,你可以使用它,真正的意思,结束它。
最后,如果您愿意,可以使用 AppLevelUseHttps 变量在需要时关闭此行为,例如测试在没有 HTTPS 的情况下是否正常工作。例如,您可以将其设置为 Web.Config 变量的值。