gpt4 book ai didi

asp.net-mvc - 在自定义授权属性中覆盖 AuthorizeCore 导致 "no suitable method found to override"错误

转载 作者:行者123 更新时间:2023-12-03 09:40:19 24 4
gpt4 key购买 nike

我正在尝试构建自定义 AuthorizeAttribute,因此在我的核心项目(类库)中,我有以下代码:

using System;
using System.Web;
using System.Web.Mvc;
using IVC.Core.Web;
using System.Linq;

namespace IVC.Core.Attributes
{
public class TimeShareAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if(!httpContext.Request.IsAuthenticated)
return false;

var rolesProvider = System.Web.Security.Roles.Providers["TimeShareRoleProvider"];

string[] roles = rolesProvider.GetRolesForUser(httpContext.User.Identity.Name);

if(roles.Contains(Website.Roles.RegisteredClient, StringComparer.OrdinalIgnoreCase))
{
return true;
}

return false;
}

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectResult("/TimeShare/Account/LogOn");

base.HandleUnauthorizedRequest(filterContext);
}
}
}

当我尝试构建事物时,出现此错误:
错误 2 'IVC.Core.Attributes.TimeShareAuthorizeAttribute.AuthorizeCore(System.Web.HttpContextBase)': 找不到合适的方法来覆盖 ...

我在这里错过了什么吗?我到处搜索,但我能找到的每个网站都告诉我完全按照我在这里所做的去做。顺便说一句,我正在使用 mvc2。
  • 编辑添加:如果我将类移动到同一解决方案中的 mvc 项目,则没有编译器错误。
  • 最佳答案

    是的,我也摸索了一段时间,然后从对象浏览器中弄清楚了。除非您一直向下滚动到用户对 HttpContextBase 类的评论,否则从 MSDN 文档中肯定不清楚。而且,当然,网络上有很多示例,但没有人显示完整的类文件! :)

    尝试将 System.Web.Abstractions 的引用添加到您的项目中。

    更新:刚刚从 MSDN 上注意到,在 v3.5 下,它在 System.Web.Abstractions 下,但在 v4 下,它在 System.Web 下。

    关于asp.net-mvc - 在自定义授权属性中覆盖 AuthorizeCore 导致 "no suitable method found to override"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3488348/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com