How to plugin a OWIN middle-ware outside of the Startup class?
I have this OWIN middle-ware which basically sets up an authentication end point for incoming requests. During our development cycle we have noticed couple of times, the remote metadata endpoint (ADFS based) going down which causes an exception while trying to configure the middle-ware on app startup. However, we want to let the app startup even if the middle-ware initiation fails and try to initialise the middle-ware at a later point. How do I do that without having access to the 'IAppBuilder' interface.
I am using the pre-built middle-ware in Katana for ADFS end point setup using the following method call -
app.UseActiveDirectoryFederationServicesBearerAuthentication(
new ActiveDirectoryFederationServicesBearerAuthenticationOptions
{
MetadataEndpoint = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"],
TokenValidationParameters = new TokenValidationParameters()
{
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
}
});
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