启动后插入OWIN中间件组件

时间:2017-07-19 07:05:49

标签: asp.net owin middleware katana

如何在Startup类之外插入OWIN中间件?

我有这个OWIN中间件,它基本上为传入请求设置了一个身份验证端点。在我们的开发周期中,我们注意到几次,远程元数据端点(基于ADFS)发生故障,导致异常,同时尝试在应用启动时配置中间件。 但是,即使中间件启动失败,我们也希望让app启动,并尝试稍后初始化中间件。如果没有访问' IAppBuilder'接口

我使用Katana中预先构建的中间件进行ADFS端点设置,使用以下方法调用 -

app.UseActiveDirectoryFederationServicesBearerAuthentication(
                    new ActiveDirectoryFederationServicesBearerAuthenticationOptions
                    {
                        MetadataEndpoint = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"],
                        TokenValidationParameters = new TokenValidationParameters()
                        {
                            ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
                        }
                    });

1 个答案:

答案 0 :(得分:1)

The OWIN infrastructure is not designed to be modified at runtime after executing the Startup code. See the Is it possible to add WsFederationAuthenticationOptions at runtime? discussion for more information.

If you face issues when a particular middleware fails, try wrapping it within a custom fake implementation and handle (failed?) initialization manually.

Check out the other related SO threads regarding this:

register new middleware to OWIN pipeline at runtime without restart application

Add Owin Pipeline Middleware after OwinStartup for new Tenant