RazorPages-尝试在RazorPage上实现操作筛选器



我想写一个自定义过滤器,检查用户是否登录到我的网站,如果没有,则将他们重定向回登录页面。

我希望过滤器在页面加载时自动应用于页面。

我已经尝试了下面显示的解决方案,但过滤器目前不起作用。

过滤器代码:

using Microsoft.AspNetCore.Mvc.Filters;
namespace MODS.Filters
{
public class AuthorisationPageFilter : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext context)
{
System.Diagnostics.Debug.Write("Filter Executed");  //write to debugger to test if working
//add real code here
base.OnActionExecuted(context);
}
}
}

接下来,这里是应用于页面模型的过滤器属性:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MODS.Filters;
namespace MODS.Pages.Menus
{
[AuthorisationPageFilter]
public class Admin_MainMenuModel : PageModel
{
public ActionResult Admin_MainMenu()
{
System.Diagnostics.Debug.Write("Function Executed");
return new ViewResult();
}
}
}

我的印象是,您需要在页面上调用一个操作/方法,以便在页面加载时应用该函数(请告诉我这是否正确(,因此以下是在.cshtml页面文件中调用Admin_MainMenu方法的代码(在剃刀页面顶部的代码块中(:

Model.Admin_MainMenu();

我目前的想法是:1.过滤器本身的类型错误(可能是IPageFilter?(2.我实现它的方式是错误的(或者我将它应用于页面模型,或者当我调用页面上的方法时(。

非常感谢您的帮助。谢谢

ActionFilterAttribute适用于MVC(控制器和操作(。对于Razor Pages,必须使用IPageFilter(IAsyncPageFilter用于异步实现(。

MVC和Razor Pages 有两种不同的过滤器管道

Razor Page过滤器IPageFilterIAsyncPageFilter允许Razor Pages在运行Razor页面处理程序之前和之后运行代码。Razor页面过滤器类似于ASP.NET Core MVC操作过滤器,只是它们不能应用于单独的页面处理程序方法。

ASP.NET Core 中Razor页面的筛选方法

它和ActionFilterAttribute一样简单。您只需要创建一个实现IPageFilterIAsyncPageFilterAttribute派生类(两者都可以(。

[AttributeUsage(AttributeTargets.Class)]
public class CustomPageFilterAttribute : Attribute, IAsyncPageFilter
{
// Executes first
public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
{
// TODO: implement this
}
// Executes last
public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
{
// Before action execution
await next();
// After action execution
}
}

现在,您可以在PageModel中使用您的属性。

[CustomPageFilter]
public class IndexModel : PageModel
{
public void OnGet() { }
}

这个答案适用于AspNet MVC而不是AspNetCore MVC,但可能对某些人有用:

如果是针对Authorization,我会使用AuthorizeAttribute类。

类似这样的东西:

using System.Web.Mvc;
namespace MODS.Filters
{
public class CustomAuthorizeUserAttribute : AuthorizeAttribute
{
// Custom property, such as Admin|User|Anon
public string AccessLevel { get; set; }
// Check to see it the user is authorized
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
System.Diagnostics.Debug.Write("Authorize Executed");  //write to debugger to test if working
// Use Core MVC Security Model
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
return false;
}
// Or use your own method of checking that the user is logged in and authorized. Returns a Boolean value.
return MySecurityHelper.CheckAccessLevel(AccessLevel);
}
// What to do when not authorized
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(
new
{
controller = "Error",
action = "NotFound"
})
);
}
}
}

然后用CustomAuthorizeUser属性装饰控制器或动作:

using MODS.Filters;
namespace MODS.Pages.Menus
{
[CustomAuthorizeUser(AccessLevel = "Admin")]
public class Admin_MainMenuModel : PageModel
{
public ActionResult Admin_MainMenu()
{
System.Diagnostics.Debug.Write("Function Executed");
return new ViewResult();
}
}
}

希望这能有所帮助!

最新更新