您的位置:首页 > 编程语言 > C#

C# 关于Attribute的初级应用

2017-10-16 14:58 357 查看
主要是在做关于角色权限验证时用到的

关于后台页面只能被管理员访问,所以单独在每个后台页面的controller上加上对应标签,[Seetting]要放在最前面,即public的前面

然后就是关于Attribute的编写:

namespace System.Web.Mvc

{

//这一行是对这个方法的实例化

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]

    public sealed class SettingAttribute : AuthorizeAttribute

    {

        //     初始化 System.Web.Mvc.SettingAttribute 类的新实例。

        //public SettingAttribute();

        public override void OnAuthorization(AuthorizationContext filterContext)

        {

            if (filterContext == null)

            {

                throw new ArgumentNullException("filterContext");

            }

            #region 判断是否为管理员

            if (!Common.CheckSettingUser())

            {

                filterContext.Result = new RedirectResult("~/Error/NoPermission");

            }

            #endregion

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: