还有另一种方法可以确保所有模块都使用 NancyFx SSL 吗?

Is there another way to ensure NancyFx SSL for all modules?

我想知道对我的 Nancy API 的所有呼叫强制执行 SSL 的正确方法。我当前的 (C#) 实现是:

public abstract class NancyHttpsModule : NancyModule
{
    public NancyHttpsModule(string baseUrl) : base(baseUrl)
    {
        this.RequiresHttps();
    }       
}

这似乎适用于非 SSL 端口上的 403 Forbidden。

有更好的方法吗? (不使用像 this article 这样的 IIS 重定向。)

您可以添加 Before Request Hook 来检查和发出重定向。在你的项目中创建一个启动任务,你不需要挂钩它,Nancy 会接它。 (伪代码如下):

public class AlwaysUseHttps : IApplicationStartUp
{
    public void Initialize(IPipelines pipelines)
    {
        pipelines.BeforeRequest.AddItemToSTartOfPiepline(RedirectIfNotHttps);
    }

    private static Response RedirectIfNotHttps(NancyContext context)
    {
       //return null if it is already https
       //else, do a redirect as shown here: https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Security/ModuleSecurity.cs
    }
}