可在[授权]属性来为所有控制器全局设置? [英] Can the [Authorize] attribute be set globally for all controllers?

查看:129
本文介绍了可在[授权]属性来为所有控制器全局设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前人把在[授权]在控制器类或具体行动。是否有可能来设置[授权]属性集中所有控制器,说要prevent非验证使用。对特定的控制器再经过一个可以定义多个特定[授权]属性,如

Currently one puts the [Authorize] on the controller class or specific action. Is it possible to setup the [Authorize] attribute centrally for all controllers, say to prevent nonauthenticated use. Then after that one could define more specific [Authorize] attributes on specific controller such as

[Authorize(Roles="Admin, SuperUser")]

感谢。

推荐答案

这应该工作(放于的Application_Start):

This should work (put in Application_Start):

GlobalFilters.Filters.Add(new AuthorizeAttribute() { Roles = "Admin, SuperUser" });

这篇关于可在[授权]属性来为所有控制器全局设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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