asp.net – ASP MVC授权所有操作除了几个
发布时间:2020-12-15 18:48:56 所属栏目:asp.Net 来源:网络整理
导读:我有一个控制器,我想要求默认除了一对夫妇之外的所有操作的授权。因此,在下面的示例中,除Index之外,所有操作都需要认证。我不想装饰每个动作与授权,我只是想在某些情况下覆盖默认授权,可能是一个自定义过滤器,如NotAuthorize。 [Authorize]public cla
我有一个控制器,我想要求默认除了一对夫妇之外的所有操作的授权。因此,在下面的示例中,除Index之外,所有操作都需要认证。我不想装饰每个动作与授权,我只是想在某些情况下覆盖默认授权,可能是一个自定义过滤器,如NotAuthorize。
[Authorize] public class HomeController : BaseController { [NotAuthorize] public ActionResult Index() { // This one wont return View(); } public ActionResult About() { // This action will require authorization return View(); } } 解决方法
好的,这是我做的。如果有更好的方式让我知道。
public class NotAuthorizeAttribute : FilterAttribute { // Does nothing,just used for decoration } public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { // Check if this action has NotAuthorizeAttribute object[] attributes = filterContext.ActionDescriptor.GetCustomAttributes(true); if (attributes.Any(a => a is NotAuthorizeAttribute)) return; // Must login if (!filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.Result = new HttpUnauthorizedResult(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET Core - 源码解析 - Program.cs (一)
- asp.net – VirtualPathUtility.ToAbsolute()VS. Url.Conte
- asp.net-mvc-3 – “update-database”的实体框架SQLMigrat
- asp.net – 从aspx页面中的Static方法访问ViewState
- asp.net-mvc – 属性似乎根本不起作用
- 混合WebForms/ASP.NET MVC
- 使用ASP.NET MVC在JS文件中设置jQuery的ajax url
- asp.net – 通过Web请求从设备获取唯一的静态ID
- asp.net – Windows Azure我可以在同一个额外的小实例或小实
- exception throw in progress runner thread_VS2015中SVN源
推荐文章
站长推荐
- asp.net – 日期字段不会在Chrome浏览器中显示该
- asp.net core EF code first
- asp.net-mvc – 你用ReSharper做什么?
- asp.net-mvc – ASP MVC区域和ActionLink
- ASP.NET应用程序从Session.Remove实现中抛出Syst
- asp.net-core-2.0 – ASP.NET MVC Core 2.0中多个
- 数组 – 如何在经典asp中拆分字符串
- 如何使用asp.net在服务器上生成新的html页面?
- 上载ASP.NET MVC Web api控制器中的文件
- asp.net – 来自web.config的Asp .Net自定义成员
热点阅读