如何让 ASP.Net Core Razor 页面符合 letsencrypt.com 对“握手”的要求?我曾尝试使用适用于 MVC 的解决方案,但路由的完成方式在 Razor Pages 中不起作用。
最佳答案
我从 this excellent tutorial 开始来自 Royal Jay 网站。 向 Web 应用程序添加路由 是我的解决方案与普通 MVC 应用程序不同的地方。由于您必须每 3 个月获得一个新的 SSL 证书,我将此解决方案设置为可配置的,这样更改 key 最终变得非常容易。
在我的 appsettings.json 文件中,我为 LetsEncrypt 添加了以下条目:
"LetsEncrypt": {
"Key": "the entire key from your letsencrypt initial session goes here"
}
此处的条目是您从 letsencrypt-auto 可执行文件(这是 Royal Jay 教程中第二个带红色下划线的部分)收到的完整 key 。
为了将配置属性传递给处理来自 LetsEncrypt 的握手的页面,我创建了一个新的接口(interface)和小类来保存 key :
接口(interface):
using System;
using System.Collections.Generic;
using System.Text;
namespace Main.Interfaces
{
public interface ILetsEncryptKey
{
string GetKey();
}
}
类:
using Main.Interfaces;
namespace Main.Models
{
public class LetsEncryptKey : ILetsEncryptKey
{
private readonly string _key;
public LetsEncryptKey(string key) => _key = key;
public string GetKey() => _key;
}
}
然后在 startup.cs 文件中,我将这些行添加到 ConfigureServices 部分:
var letsEncryptInitialKey = Configuration["LetsEncrypt:Key"];
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/LetsEncrypt", $".well-known/acme-challenge/{letsEncryptInitialKey.Split('.')[0]}");
});
services.AddSingleton<ILetsEncryptKey>(l => new LetsEncryptKey(letsEncryptInitialKey));
现在我们唯一要做的就是创建将处理握手请求并返回响应的页面。
LetsEncrypt.cshtml.cs:
using Main.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RazorPages.Pages
{
public class LetsEncryptModel : PageModel
{
private readonly ILetsEncryptKey _letsEncryptKey;
public LetsEncryptModel(ILetsEncryptKey letsEncryptKey)
{
_letsEncryptKey = letsEncryptKey;
}
public ContentResult OnGet()
{
var result = new ContentResult
{
ContentType = "text/plain",
Content = _letsEncryptKey.GetKey()
};
return result;
}
}
}
关于asp.net-core-2.0 - 如何让 LetsEncrypt 在 ASP.Net Core Razor Pages 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48272373/