【问题标题】:How do I get LetsEncrypt working in ASP.Net Core Razor Pages?如何让 LetsEncrypt 在 ASP.Net Core Razor Pages 中工作?
【发布时间】:2018-06-24 15:18:30
【问题描述】:

如何让 ASP.Net Core Razor 页面符合letsencrypt.com 对“握手”的要求?我曾尝试使用适用于 MVC 的解决方案,但完成路由的方式在 Razor Pages 中不起作用。

【问题讨论】:

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


    【解决方案1】:

    我从 Royal Jay 网站的 this excellent tutorial 开始。 向 Web 应用程序添加路由是我的解决方案与普通 MVC 应用程序不同的地方。由于您必须每 3 个月获得一个新的 SSL 证书,因此我将此解决方案配置为可配置,以便最终更改密钥非常容易。

    在我的 appsettings.json 文件中,我为 LetsEncrypt 添加了以下条目:

    "LetsEncrypt": {
        "Key": "the entire key from your letsencrypt initial session goes here"
      }
    

    此处的条目是您从 letsencrypt-auto 可执行文件中收到的整个密钥(它是 Royal Jay 教程中的第二个红色下划线部分)。

    为了将配置属性传递到将处理来自 LetsEncrypt 的握手的页面,我创建了一个新接口和一个小类来保存密钥:

    界面:

    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;
            }
        }
    }
    

    【讨论】:

    • 我如何设置为 www 和没有 www 设置它
    猜你喜欢
    • 2018-10-29
    • 2020-11-21
    • 2018-03-30
    • 2021-04-28
    • 1970-01-01
    • 2019-04-05
    • 2018-10-16
    • 2019-07-25
    • 2018-06-07
    相关资源
    最近更新 更多