asp.net-core-2.0 - 如何让 LetsEncrypt 在 ASP.Net Core Razor Pages 中工作?

标签 asp.net-core-2.0 lets-encrypt razor-pages

如何让 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/

相关文章:

c# - Google 驱动器重定向 URI 不匹配以及如何从 ASP.net Core 2.0 中的谷歌驱动器获取文件列表

logging - 是否可以在 .NET Core ConsoleLogger 和 DebugLogger 中禁用类别输出?

asp.net-mvc - 在启动/程序设置中设置允许的最大内容长度限制不起作用

ubuntu - letsencrypt ssl 安装快捷方式

ssl - 未捕获( promise )DOMException : Failed to register a ServiceWorker: An SSL certificate error occurred when fetching the script

c# - Razor 页面,表单页面处理程序无法与GET方法一起使用

visual-studio - ASP.NET Core 2 上未定义 runco​​mmand 属性

ssl - letsencrypt似乎正在将域名重定向到81.21.76.62

c# - 使用 Razor 页面返回 XML?

html - Bootstrap-4 验证 : Add border and background-color to form input-group