mvc继承BaseController实现页面访问权限控制

创建BaseController.cs ,重写Initialize()方法

protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            bool IsOK = false;
            //获取当前访问页面地址
            string requestPath = RequestHelper.GetScriptName;
            string[] filterUrl = { //无需授权能打开的界面

            };//过滤特别页面
            //对上传的文件的类型进行一个个匹对
            for (int i = 0; i < filterUrl.Length; i++)
            {
                if (requestPath == filterUrl[i])
                {
                    IsOK = true;
                    break;
                }
            }
            if (!IsOK)
            {
                string UserId = RequestSession.GetSessionUser().UserId.ToString();//用户ID
                DataTable dt = sys_idao.GetPermission_URL(UserId);
                DataView dv = new DataView(dt);
                dv.RowFilter = "NavigateUrl = '" + requestPath + "'";
                if (dv.Count == 0)
                {
                    StringBuilder strHTML = new StringBuilder();
                    strHTML.Append("
"); strHTML.Append("权限不足
"); requestContext.HttpContext.Response.Write(strHTML); requestContext.HttpContext.Response.End(); } } }
其他Controller继承BaseController就能实现页面的权限控制

你可能感兴趣的:(C#,MVC,mvc,权限,BaseController)