c# - How do i set up a default for ActiveAuthenticationSchemes? -
in asp.net core 2 project, have custom authenticationhandler
middleware want plug in.
public class basicauthenticationmiddleware : authenticationhandler<authenticationschemeoptions> { public basicauthenticationmiddleware(ioptionsmonitor<authenticationschemeoptions> options, iloggerfactory logger, urlencoder encoder, isystemclock clock) : base(options, logger, encoder, clock) { } protected override task<authenticateresult> handleauthenticateasync() { var principal = new genericprincipal(new genericidentity("user"), null); var ticket = new authenticationticket(principal, new authenticationproperties(), "basicauth"); return task.fromresult(authenticateresult.success(ticket)); } }
in startup have following:
public void configureservices(iservicecollection services) { services.addmvc(); services.addauthentication(options => { options.defaultauthenticatescheme = "basicauth"; options.defaultchallengescheme = "basicauth"; options.addscheme("basicauth", x => { x.displayname = "basicauthenticationmiddleware"; x.handlertype = typeof(basicauthenticationmiddleware); }); }); }
and view controller:
[route("api/[controller]")] public class valuescontroller : controller { // api/values/works [httpget] [route("works")] [authorize(activeauthenticationschemes = "basicauth")] public string works() { return "works"; } // api/values/doesnotwork [httpget] [route("doesnotwork")] [authorize] public string doesnotwork() { return "does not work"; } }
my authenticator handleauthenticateasync
called when specify activeauthenticationschemes
scheme name, otherwise not. have demo app showing behavior here: https://github.com/johnpaguirre/authenticationschemaproblem
i want basicauthenticationmiddleware
log in demo logic. how can make activeauthenticationschemes
default "basicauth" requests?
anyone have ideas on missing?
i don't think can set default, have other options.
create own custom authorisation attribute:
public class basicauthauthorizeattribute : authorizeattribute { public basicauthauthorizeattribute() { activeauthenticationschemes = "basicauth"; } }
and use on actions before:
[basicauthauthorize] public string someaction() { //snip }
add
authorize
attribute actions , override needed. that, in `` method:public void configureservices(iservicecollection services) { services.addmvc(options => { options.filters.add(new authorizeattribute { activeauthenticationschemes = "basicauth" }); }); //snip }
and overriding it:
[allowanonymous] public string unsecureaction() { //snip }
Comments
Post a Comment