覆盖AuthorizeCore定制属性授权结果"发现越权QUOT没有合适的方法;错误 [英] Overriding AuthorizeCore in custom Authorize attribute results in "no suitable method found to override" error

查看:188
本文介绍了覆盖AuthorizeCore定制属性授权结果"发现越权QUOT没有合适的方法;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建自定义AuthorizeAttribute,所以在我的核心项目(类库)我有这个code:

I'm trying to build custom AuthorizeAttribute, so in my Core project (a class library) I have this code:

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):没有合适的方法找到重写 ...

When I try to build the thing I get this error: Error 2 'IVC.Core.Attributes.TimeShareAuthorizeAttribute.AuthorizeCore(System.Web.HttpContextBase)': no suitable method found to override ...

我失去了一些东西在这里?我已经找遍了,但每一个网站,我可以找到只是告诉我做什么我在这里做。我使用MVC2顺便说一句。

Am I missing something here? I've searched all over but every site I can find just tells me to do exactly what I did here. I'm using mvc2 btw.


  • 编辑补充:如果我移动类的MVC项目在同一个解决方案没有编译器错误

推荐答案

是的,我摸索与一期一会也和从对象浏览器了它。它,除非你滚动一路下跌到上HttpContextBase类用户评论肯定不是从MSDN文档清晰。并且,当然,很多在网络上的例子,但从来没有人显示了完整的类文件! :)

Yeah, I fumbled with that one for a while too and figured it out from the Object browser. It certainly is NOT clear from the MSDN docs unless you scroll all the way down to the user comments on the HttpContextBase class. And, of course, lots of examples on the web, but nobody ever shows the full class file! :)

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

Try adding a reference to System.Web.Abstractions to your project.

更新:从MSDN,根据v3.5版本,它是根据System.Web.Abstractions只注意到,但在第4版,它的System.Web下

UPDATE: Just noticed from the MSDN that under v3.5, it is under System.Web.Abstractions, but under v4, it's under System.Web.

这篇关于覆盖AuthorizeCore定制属性授权结果"发现越权QUOT没有合适的方法;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆