zoukankan      html  css  js  c++  java
  • ASP.NET最简单的用户权限管理

    前些时间看到中国微软做的FrienDev开源项目,发现他们有个思路做用户权限管理的方法。首先在网站上面建几个需要权限才可以访问的目录,再建一个就是不需要权限就可以访问的目录,例如:需要权限的会员管理页面:Member,公共页面:Public

    然后添加一个空项目StBusiness进来,添加一个类AuthenticationModule,再做一个ApplicationSettings.cs类,用来记录文件的路径与常量,在AuthenticationModule类里面继承IHttpModule接口

    public class AuthenticationModule : IHttpModule
     {

    }

    在类里添加初始化方法

    public void Init(HttpApplication context)
            {
                context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
            }

    添加测试过程

    private void context_AcquireRequestState(object sender, EventArgs e)
            {
                HttpContext context = HttpContext.Current;
                string path = context.Request.Path.ToLower();

                // 只处理aspx文件,因为其他文件无法获得Session对象,无法判断是否已经登录
                if (path.EndsWith(".aspx"))
                {
                    // 如果用户没有登录就會返回false
                    if (!UserRules.Instance.IsCurrentUserLogined)
                    {
                        // 对于公共文件夹和根目录的文件不做判断
                        if (path.StartsWith("/" + ApplicationSettings.PUBLICFOLDERNAME + "/")==false && !(path.LastIndexOf("/") == 0))
                        {
                            // 跳转到公共页面首页                       
                            context.Response.Redirect(ApplicationSettings.PUBLICLOGOUTFILENAME, false);
                            context.ApplicationInstance.CompleteRequest();
                        }
                    }
                    else  //登陆了再查看是587还是普通用户
                    {
                        if (path.ToLower() == ApplicationSettings.MemberAe.ToLower() || path == ApplicationSettings.MemberSe.ToLower())
                        {
                            if (context.Session[ApplicationSettings.SESSIONUSERIDKEY].ToString() != "587")
                            {
                                // 跳转到原来页面                       
                                context.Response.Redirect(ApplicationSettings.MemberStm, false);
                                context.ApplicationInstance.CompleteRequest();
                            }
                        }
                    }
                }
            }

    在代码上面用到了一个检查是否登陆的方法UserRules.Instance.IsCurrentUserLogined

    添加UserRules类,用户登陆用单例模式来实现代码如下:

    public class UserRules
        {
            private static UserRules _instance;

            public static UserRules Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        _instance = new UserRules();
                    }
                    return _instance;
                }
            }

            private UserRules()
            {

            }

    }

    然后再在UserRules类里面添加IsCurrentUserLogined方法

    public bool IsCurrentUserLogined
            {
                get
                {
                    if (HttpContext.Current.Session["UID"] == null)
                    {                    
                        return false;
                    }
                    return true;
                }
            }

    最后一步,就是在web.congif里面配置

    <httpModules>
               <add name="AuthenticationModule" type="StBusiness.AuthenticationModule, StBusiness"/>
    </httpModules>

    这样就完成的一个最简单的用户权限管理功能,发觉对小型简单的网站来说,这样不愧是好办法。

  • 相关阅读:
    Hibernate save, saveOrUpdate, persist, merge, update 区别
    Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目
    Neo4j批量插入(Batch Insertion)
    嵌入式(Embedded)Neo4j数据库访问方法
    Neo4j 查询已经创建的索引与约束
    Neo4j 两种索引Legacy Index与Schema Index区别
    spring data jpa hibernate jpa 三者之间的关系
    maven web project打包为war包,目录结构的变化
    创建一个maven web project
    Linux下部署solrCloud
  • 原文地址:https://www.cnblogs.com/whtydn/p/1447133.html
Copyright © 2011-2022 走看看