2017-07-31 72 views
0

認證可以南希模塊通過模塊的構造函數調用RequiresAuthentication啓用:如何開啓驗證南希模塊,默認行爲

public class CustomModule : NancyModule 
{ 
    public ConfigurationModule() 
    { 
     this.RequiresAuthentication(); 

     // ... specify module 
    } 
} 

是否有可能啓用每個默認的身份驗證,並添加了禁用它支持?

我知道可以繼承NancyModule並使用子類代替,但這不是我想要的方式。我想加載不引用我的自定義程序集的模塊。

回答

0

不幸的是,我沒有找到一個沒有引用自定義程序集的解決方案。

最後,我想出了以下解決方案:

public abstract class CustomNancyModule : NancyModule 
{ 
    protected CustomNancyModule(bool requiresAuthentication = true, string modulePath = null) 
     : base(modulePath ?? string.Empty) 
    { 
     if (requiresAuthentication) 
     { 
      this.RequiresAuthentication(); 
     } 
    } 
} 

,並確保所有的模塊從CustomNancyModule得到對應的引導程序執行:

public class CustomBootstrapper : DefaultNancyBootstrapper 
{ 
    protected override IEnumerable<ModuleRegistration> Modules 
    { 
     get 
     { 
      var modules = base.Modules; 
      var customModuleType = typeof(CustomNancyModule); 

      foreach (var module in base.Modules) 
      { 
       if (!module.ModuleType.IsSubclassOf(customModuleType)) 
        throw new InvalidOperationException(
         $"Module '{module.ModuleType}' is not a sub class of '{customModuleType}'. It is not allowed to derive directly from NancyModule. Please use '{customModuleType}' as base class for your module."); 
      } 
      return modules; 
     } 
    } 
}