c# - Redirect to error page if hangfire authorization fails in asp.net mvc5 application -


can redirect user trying browse hangfire url unauthorized page. using asp.net mvc 5. have following page in startup.cs file.

 public void configuration(iappbuilder app)         {             string conn = system.configuration.configurationmanager.     connectionstrings["conn"].connectionstring;             // more information on how configure application, visit http://go.microsoft.com/fwlink/?linkid=316888             globalconfiguration.configuration                .usesqlserverstorage(conn,                new sqlserverstorageoptions { queuepollinterval = timespan.fromseconds(1) });              //backgroundjob.enqueue(() => console.writeline("fire-and-forget!"));              //app.usehangfiredashboard();              app.usehangfiredashboard("/admin/hangfire", new dashboardoptions             {                 authorization =new[] { new dashboardauthorizationfilter() }             });             //app.maphangfiredashboard("/hangfire", new[] { new authorizationfilter() });             app.usehangfireserver();             //start hangfire recurring jobs             hangfireservices service = new hangfireservices();             //service.startarchive();             service.startdelete();         } 

the hangfireservices has jobs:

public void startdelete()         {             list<keyvaluepair<string, int>> c = _service.getserviceretention();              foreach (var obj in c)             {                   recurringjob.addorupdate(delete_service + obj.key, () =>                   delete(obj.key), //this function actual process                cron.dayinterval(convert.toint32(obj.value)));             }         } 

the authorization code :

public class dashboardauthorizationfilter : idashboardauthorizationfilter     {         public bool authorize(dashboardcontext context)         {             //todo:implement                  return false;         }     } 

the default page home page on different authorization class set up. user fails authorization rules per db , redirected unauthorizedcontroller index page. if user manually changes url point /hangfire,as authorization returned false, sees blank page, want redirect unauthorizedcontroller index page.

if want redirect particular page on controller, may help.

i created account controller login method follows:

    [httppost]     [allowanonymous]     [validateantiforgerytoken]     public async task<actionresult> login(loginviewmodel model, string returnurl = "jobs")     {         if (!modelstate.isvalid)         {             return view(model);         }              var user = await usermanager.findbynameasync(model.username);              await signinmanager.signinasync(user, false, false);                      var virtualdirectory = request.applicationpath.equals("/") ? "/" : request.applicationpath + "/";                     return redirect(virtualdirectory + returnurl);         }          modelstate.addmodelerror("", "invalid login attempt.");         return view(model);     } 

in startup.auth.cs, did following changes:

        public void configureauth(iappbuilder app)         {             app.createperowincontext(applicationdbcontext.create);             app.createperowincontext<applicationusermanager>              (applicationusermanager.create);             app.createperowincontext<applicationsigninmanager>               (applicationsigninmanager.create);               // configure sign in cookie             app.usecookieauthentication(new cookieauthenticationoptions             {                 authenticationtype = defaultauthenticationtypes.applicationcookie,                 cookiename = "testservice",                 loginpath = new pathstring("/account/login"),                 slidingexpiration = true,                 expiretimespan = timespan.fromminutes(20000),                 provider = new cookieauthenticationprovider()             });         } 

and finally, in startup.cs class:

public partial class startup     {             public void configuration(iappbuilder app)         {              app.usecookieauthentication(new cookieauthenticationoptions             {                 authenticationtype = defaultauthenticationtypes.applicationcookie,                 loginpath = new pathstring("/account/login")             });               logprovider.setcurrentlogprovider(new hangfirelogprovider());              globalconfiguration.configuration.usesqlserverstorage("hangfirepersistence");              app.usehangfiredashboard("/jobs", new dashboardoptions             {                 authorization = new[] { new hangfireauthfilter() }             });              app.usehangfireserver();              configureauth(app);         }     }  public class hangfireauthfilter : idashboardauthorizationfilter {     public bool authorize(dashboardcontext context)     {         var user = httpcontext.current.user;          return user != null && user.isinrole("admin") && user.identity.isauthenticated;     } } 

when ever not authenticated, redirected login action on account controller.


Comments