c# - Separate configuration of two instances of same type of middleware -


in asp.net core 1.1 mvc application have need 2 pieces of jwtbearerauthentication middleware different options.

so have 2 calls in startup class:

app.usejwtbearerauthentication(new jwtbeareroptions{...}); app.usejwtbearerauthentication(new jwtbeareroptions{...}); 

the jwtbeareroptions configuration becomes messy in startup class, both have events registered well, best way separate configuration of these 2 pieces of middleware out different classes?

i thought of creating factory , spitting them out, hoping there feature of di , configuration api might have missed...

edit

ok got using factory pattern, works fine. way of doing it?

startup

public startup(ihostingenvironment env)     {         var builder = new configurationbuilder()             .setbasepath(env.contentrootpath)             .addjsonfile("appsettings.json", optional: false, reloadonchange: true)             .addjsonfile($"appsettings.{env.environmentname}.json", optional: true)             .addenvironmentvariables();         configuration = builder.build();     }      public iconfigurationroot configuration { get; }      // method gets called runtime. use method add services container.     public void configureservices(iservicecollection services)     {         services.addsingleton<jwtoptionsfactory>();         services.addmvc();         services.addauthentication();         services.addauthorization(options =>         {             options.addpolicy("systempolicy", policy =>             {                 policy.authenticationschemes = new list<string> { "system" };                 policy.requireauthenticateduser();             });             options.addpolicy("userpolicy", policy =>             {                 policy.authenticationschemes = new list<string> { "user" };                 policy.requireauthenticateduser();             });         });     }      // method gets called runtime. use method configure http request pipeline.     public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory, jwtoptionsfactory optionsfactory)     {         loggerfactory.addconsole(configuration.getsection("logging"));         loggerfactory.adddebug();         app.usejwtbearerauthentication(optionsfactory.getsystemjwtoptions());         app.usejwtbearerauthentication(optionsfactory.getuserjwtoptions());         app.usemvc();     } } 

jwtoptionsfactory

public class jwtoptionsfactory {     private readonly ilogger logger;      public jwtoptionsfactory(ilogger<jwtoptionsfactory> logger)     {         this.logger = logger;     }      public jwtbeareroptions getuserjwtoptions()     {         jwtbeareroptions options = new jwtbeareroptions {             authenticationscheme = "user",             automaticauthenticate = false,             automaticchallenge = false,             authority = "...",             audience = "...",             events = new jwtbearerevents             {                 ontokenvalidated = onusertokenvalidated             }         };         return options;     }      public jwtbeareroptions getsystemjwtoptions()     {         jwtbeareroptions options = new jwtbeareroptions         {             authenticationscheme = "system",             automaticauthenticate = false,             automaticchallenge = false,             authority = "...",             audience = "...",             events = new jwtbearerevents             {                 ontokenvalidated = onsystemtokenvalidated             }         };         return options;     }      public async task onusertokenvalidated(tokenvalidatedcontext context)     {         logger.loginformation("user identity validated yay!!");     }      public async task onsystemtokenvalidated(tokenvalidatedcontext context)     {         logger.loginformation("system identity validated yay!!");     } } 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -