2017-04-05 1 views
1

ハングファイアのURLを閲覧しようとしているユーザーを権限のないページにリダイレクトできますか? 私はstartup.csファイルに次のページを持っています。asp.net mvc5アプリケーションでhangfireの認証が失敗した場合、エラーページにリダイレクト

public void Configuration(IAppBuilder app) 
     { 
      String conn = System.Configuration.ConfigurationManager. 
    ConnectionStrings["conn"].ConnectionString; 
      // For more information on how to configure your 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(); 
     } 

HangFireServicesは仕事を持っています

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 is my function that does the actual process 
       Cron.DayInterval(Convert.ToInt32(obj.Value))); 
      } 
     } 

認証コードは次のとおりです。

public class DashboardAuthorizationFilter : IDashboardAuthorizationFilter 
    { 
     public bool Authorize(DashboardContext context) 
     { 
      //TODO:Implement 

       return false; 
     } 
    } 

デフォルトページが異なる承認クラスが設定されているホームページです。ユーザーはdbごとに承認ルールに失敗し、UnAuthorizedControllerのインデックスページにリダイレクトされます。ユーザーが/ hangfireを指すようにURLを手動で変更した場合、返される権限はfalseであるため、空白のページが表示されますが、UnAuthorizedControllerのインデックスページにリダイレクトします。

+0

明確にするために、ユーザーには常に「401」ページが表示されるようにしたいのですか、またはそれらがHangfire UIにログインできない場合にのみ表示しますか? – G0dsquad

+0

ユーザーが承認に失敗した場合のみ、401またはカスタムエラーページを表示しますか? – anu

+0

私はweb.configの 'httpError'エントリでこれを試しましたが、ログイン失敗時でさえ何らかの理由でこれを無視するようです。うまくいけば、誰かがこれがどのように傍受されるのか知っていたらうれしいです! – G0dsquad

答えて

0

コントローラーの特定のページにリダイレクトしたい場合は、これが役立つかもしれません。私Startup.Auth.csで

[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); 
    } 

、私は次のようでした変更:

 public void ConfigureAuth(IAppBuilder app) 
     { 
      app.CreatePerOwinContext(ApplicationDbContext.Create); 
      app.CreatePerOwinContext<ApplicationUserManager> 
      (ApplicationUserManager.Create); 
      app.CreatePerOwinContext<ApplicationSignInManager> 
       (ApplicationSignInManager.Create); 


      // Configure the 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() 
      }); 
     } 

そして最後に、startup.csで次のように

私はloginメソッドを持つアカウントコントローラを作成しましたクラス:

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; 
    } 
} 

あなたがこれまでに認証されていない場合は、次のようなログインアクションにリダイレクトされます。 ccountコントローラ。

関連する問題