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); } } } 当我尝试构建的东西我得到这个错误: 我在这里缺少什么?我已经搜索过,但我可以找到的每个网站只是告诉我做我在这里做的。我正在使用mvc2 btw。 >编辑添加:如果我在同一个解决方案中将类移动到mvc项目,那么就没有编译错误。 解决方法
是的,我摸索了一个一会儿,并从对象浏览器中找出来。除非您一直滚动到HttpContextBase类上的用户评论,否则MSDN文档当然不清楚。当然,网络上有很多例子,但没有人会显示完整的类文件! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 与ASP.net WEB API进行交易
- 实现ASP.NET多文件上传程序代码
- asp-classic – 如何从Classic ASP Request获取* actual *
- asp.net-mvc-3 – SelectList不显示所选项目
- asp.net – 是否可以完全在代码中配置ELMAH?
- asp.net – Fulltext Query String的全文查询参数无效
- asp.net – 在打开和打印多个记录时强制HTML / ASP中的分页
- asp.net-mvc – ASP.NET MVC 3远程验证允许原始值
- asp.net – 在VS 2010中发布到Azure时出错 – 对象引用未设
- asp.net-mvc-3 – MVC:路由获取/发布到不同的控制器 怎么样
推荐文章
站长推荐
- asp.net-mvc – 使用AutoMapper的控制器上的单元
- 想想就好:我感觉我欠下的代码,再写十年也写不完
- 在ASP和ASP.Net之间共享登录系统
- asp.net – How2:在HttpModule中挂钩的事件,用于
- asp.net-core – 有没有办法将ASP.NET Core应用程
- asp.net – User.Identity.GetUserId()方法无法在
- asp.net-mvc-5 – “确认密码”和“密码”不匹配
- asp.net – Webforms与MVC.一旦你开始使用MVC ..
- asp.net – Html.RenderAction和Html.Action之间
- 限制面向公众的asp.net应用程序演示站点?
热点阅读