How do I setup multiple auth schemes in ASP.NET Core 2.0? -


i'm trying migrate auth stuff core 2.0 , having issue using own authentication scheme. service setup in startup looks this:

var authenticationbuilder = services.addauthentication(options => {     options.addscheme("myauth", builder =>     {         builder.handlertype = typeof(cookieauthenticationhandler);     }); })     .addcookie(); 

my login code in controller looks this:

var claims = new list<claim> {     new claim(claimtypes.name, user.name) };  var props = new authenticationproperties {     ispersistent = persistcookie,     expiresutc = datetime.utcnow.addyears(1) };  var id = new claimsidentity(claims); await httpcontext.signinasync("myauth", new claimsprincipal(id), props); 

but when i'm in controller or action filter, have 1 identity, , it's not authenticated one:

var identity = context.httpcontext.user.identities.singleordefault(x => x.authenticationtype == "myauth"); 

navigating these changes has been difficult, i'm guessing i'm doing .addscheme wrong. suggestions?

edit: here's (essentially) clean app results not in 2 sets of identities on user.identies:

namespace webapplication1.controllers {     public class testy : controller     {         public iactionresult index()         {             var = httpcontext.user.identities;             return content("index");         }          public async task<iactionresult> in1()         {             var claims = new list<claim> { new claim(claimtypes.name, "in1 name") };             var props = new authenticationproperties  { ispersistent = true, expiresutc = datetime.utcnow.addyears(1) };             var id = new claimsidentity(claims);             await httpcontext.signinasync(cookieauthenticationdefaults.authenticationscheme, new claimsprincipal(id), props);             return content("in1");         }          public async task<iactionresult> in2()         {             var claims = new list<claim> { new claim(claimtypes.name, "a2 name") };             var props = new authenticationproperties { ispersistent = true, expiresutc = datetime.utcnow.addyears(1) };             var id = new claimsidentity(claims);             await httpcontext.signinasync("a2", new claimsprincipal(id), props);             return content("in2");         }          public async task<iactionresult> out1()         {             await httpcontext.signoutasync(cookieauthenticationdefaults.authenticationscheme);             return content("out1");         }          public async task<iactionresult> out2()         {             await httpcontext.signoutasync("a2");             return content("out2");         }     } } 

and startup:

namespace webapplication1 {     public class startup     {         public startup(iconfiguration configuration)         {             configuration = configuration;         }          public iconfiguration configuration { get; }          public void configureservices(iservicecollection services)         {             services.addauthentication(options =>             {                 options.defaultscheme = cookieauthenticationdefaults.authenticationscheme;                 })                 .addcookie(cookieauthenticationdefaults.authenticationscheme)                 .addcookie("a2");              services.addmvc();         }          public void configure(iapplicationbuilder app, ihostingenvironment env)         {             app.useauthentication();              app.usemvc(routes =>             {                 routes.maproute(name: "default", template: "{controller=home}/{action=index}/{id?}");             });         }     } } 

navigating these changes has been difficult, i'm guessing i'm doing .addscheme wrong.

don't use addscheme: it's low-level method designed handlers writers.

how setup multiple auth schemes in asp.net core 2.0?

to register cookies handler, do:

public class startup {     public void configureservices(iservicecollection services)     {         services.addauthentication(options =>         {             options.defaultscheme = "myauth1";         })         .addcookie("myauth1");        .addcookie("myauth2");     }      public void configure(iapplicationbuilder app)     {         app.useauthentication();          // ...     } } 

it's important note can't register multiple default schemes in 1.x (the whole point of huge refactoring avoid having multiple automatic authentication middleware @ same time).

if absolutely need emulate behavior in 2.0, can write custom middleware manually calls authenticateasync() , creates claimsprincipal containing identities need:

public class startup {     public void configureservices(iservicecollection services)     {         services.addauthentication(options =>         {             options.defaultscheme = "myauth1";         })         .addcookie("myauth1");        .addcookie("myauth2");     }      public void configure(iapplicationbuilder app)     {         app.useauthentication();          app.use(async (context, next) =>         {             var principal = new claimsprincipal();              var result1 = await context.authenticateasync("myauth1");             if (result1?.principal != null)             {                 principal.addidentities(result1.principal.identities);             }              var result2 = await context.authenticateasync("myauth2");             if (result2?.principal != null)             {                 principal.addidentities(result2.principal.identities);             }              context.user = principal;              await next();         });          // ...     } } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -