Razor Pages - 试图在Razor页面上实现动作过滤器

问题描述 投票:3回答:1

我想编写一个自定义过滤器,用于检查用户是否登录到我的站点,如果不是,则将其重定向回登录页面。

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

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

过滤代码:

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。我实现它的方式是错误的(无论我将其应用于页面模型,还是当我在页面上调用方法)。

任何帮助是极大的赞赏。谢谢。

c# razor filter razor-pages action-filter
1个回答
0
投票

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

如果是授权,我会使用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属性装饰Controller或Action:

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

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.