zoukankan      html  css  js  c++  java
  • MVC巧用枚举做权限管理

    用户数据表,每个用户有一个或者多个权限,用户表如下

    userid,roleid,username等

    权限枚举如下:

    public class CustomEnum
        {
            [Flags]
            /// <summary>
            /// 用户角色枚举
            /// </summary>
            public enum UserRole
            {
                /// <summary>
                /// 游客
                /// </summary>
                Visitor = 1,
                /// <summary>
                /// 学生
                /// </summary>
                Student = 2,
                /// <summary>
                /// 老师
                /// </summary>
                ///
                Teacher = 4,
                /// <summary>
                /// 家长
                /// </summary>
                Parents = 8,
                /// <summary>
                /// 加盟商
                /// </summary>
                Agents = 16
            }
        }

    为以下对象权限对象赋值

      /// <summary>
            /// 用户角色
            /// </summary>
            public static CustomEnum.UserRole UserRole
            {
                get
                {
                    if (!string.IsNullOrEmpty(SessionAction.ReadSession("xxb_WebUserRole")))
                    {
                        return (CustomEnum.UserRole)Enum.Parse(typeof(CustomEnum.UserRole), SessionAction.ReadSession("xxb_WebUserRole"));
                      }
                    else
                    {
                        return CustomEnum.UserRole.Visitor;
                    }
                }
                set
                {
                    System.Web.HttpContext.Current.Session["xxb_WebUserRole"] = (CustomEnum.UserRole)Enum.Parse(typeof(CustomEnum.UserRole), value.ToString());

                }
            }

    用以下语句判断是否属于某种权限,并进行某种操作

     if (WebSession.UserRole == CustomEnum.UserRole.Visitor) //如果是普通游客 
                    {
                        //跳转到登陆页面
                        filterContext.Result = new RedirectResult("/User/LogOn?ReturnUrl=" + filterContext.HttpContext.Request.Url);
                    }
                    else
                    {
                        //跳转到权限提醒页面
                        filterContext.Result = new RedirectResult("/Home/Role?ReturnUrl=" + filterContext.HttpContext.Request.Url);
                    }

  • 相关阅读:
    GAN阶段性小结(损失函数、收敛性分析、训练技巧、应用“高分辨率、domain2domain”、研究方向)
    MMD :maximum mean discrepancy
    Chinese-BERT-wwm
    VGG16学习笔记
    jupyter notebook使用技巧--命令行模式以及在Markdown模式使用的操作(4)
    不同领域公开数据集下载
    数据挖掘竞赛,算法刷题网址汇总
    在keras下实现多个模型的融合
    问题集合
    开源测试数据集合
  • 原文地址:https://www.cnblogs.com/lori/p/2196638.html
Copyright © 2011-2022 走看看