加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在自定义授权属性中覆盖AuthorizeCore导致“找不

发布时间:2020-12-15 18:48:48 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试构建自定义的AuthorizeAttribute,所以在我的Core项目(一个类库)中,我有这个代码: using System;using System.Web;using System.Web.Mvc;using IVC.Core.Web;using System.Linq;namespace IVC.Core.Attributes{ public class TimeShareAuthorize
我正在尝试构建自定义的AuthorizeAttribute,所以在我的Core项目(一个类库)中,我有这个代码:
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 btw。

>编辑添加:如果我在同一个解决方案中将类移动到mvc项目,那么就没有编译错误。

解决方法

是的,我摸索了一个一会儿,并从对象浏览器中找出来。除非您一直滚动到HttpContextBase类上的用户评论,否则MSDN文档当然不清楚。当然,网络上有很多例子,但没有人会显示完整的类文件!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读