扩展NETMEMBERSHIP权限功能.docx

上传人:b****3 文档编号:11540845 上传时间:2023-06-01 格式:DOCX 页数:22 大小:95.36KB
下载 相关 举报
扩展NETMEMBERSHIP权限功能.docx_第1页
第1页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第2页
第2页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第3页
第3页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第4页
第4页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第5页
第5页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第6页
第6页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第7页
第7页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第8页
第8页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第9页
第9页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第10页
第10页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第11页
第11页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第12页
第12页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第13页
第13页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第14页
第14页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第15页
第15页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第16页
第16页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第17页
第17页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第18页
第18页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第19页
第19页 / 共22页
扩展NETMEMBERSHIP权限功能.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

扩展NETMEMBERSHIP权限功能.docx

《扩展NETMEMBERSHIP权限功能.docx》由会员分享,可在线阅读,更多相关《扩展NETMEMBERSHIP权限功能.docx(22页珍藏版)》请在冰点文库上搜索。

扩展NETMEMBERSHIP权限功能.docx

扩展NETMEMBERSHIP权限功能

扩展ASP.NETMEMBERSHIP权限功能

(一)

  扩展ASP.NETMEMBERSHIP权限功能

  目前常用的ASP.NET的membership功能,可以将权限控制到页,需要我们在web.config中进行设置,如果需求稍多一点,你会发现有些力不从心,网上有很多开源的权限管理系统,但差不多都是重新设计开发,所以我这里要做的是在membership的基础上增加用户组权限到每个页面及按钮的功能,如A组只能新增,他的页面上面就只有新增按钮,B组只能删除和修改他的界面上就没有新增的功能,即使B组知道新增的URL,进入后也会提示无权限,各个功能都能过后台数据库进行配置,全局来控制。

   membership的用法不用多说,下面讲讲思路

  1.当用户访问文件目录,读取当前目录下面的web.config

  2.根据web.config中的设定每个文件权限,并匹配数据库中的信息,如果符合条件显示按钮或页面,不成立隐藏或提示

   1.数据库

  创建表

  aspnet_Ex_PermissionsForRoles

  

   创建视图

  vw_aspnet_Ex_PermissionsForRoles

  

     创建存储过程

  aspnet_Ex_GetPermissionByRoles

  

代码

CREATE PROCEDURE [dbo].[aspnet_Ex_GetPermissionByRoles]

    roles                nvarchar(max)=NULL,

    path                nvarchar(max)=NULL

AS

BEGIN

    SET NOCOUNT ON;

    declare sql nvarchar(2000)

    set sql='select * from [vw_aspnet_Ex_PermissionsForRoles] where RoleName in('+roles+') and ModulePath='''+path+''''

    if roles is not null

        EXEC sp_executesql sql

END

   数据库准备完毕

   下面是结构

  

  

 

   项目文件结构

  

 扩展ASP.NETMEMBERSHIP权限功能

(二)

  前一篇介绍了需求,和数据库结构与和项目结构

   这一篇主要介绍一下结构和配置

  admin下面的web.config

xmlversion="1.0"?

>

 

   

 

   

   

 

   

     

"/>

   

 

    EC.Permissions.Config主要是实现了IConfigurationSectionHandler接口,来完成自定义的配置

    admin/test下面web.config  

xml version="1.0"?

>

xmlversion="1.0"?

>

 

   account

   test

   

     

       Default.aspx

       列表

       2

     

     

       Default.aspx

       编辑

       16

     

     

       Default.aspx

       删除

       32

     

     

       Ok.aspx

       测试

       2

     

   

 

    1.串行化web.config

  EC.Permissions.Config类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Configuration;

using System.Xml;

using System.Xml.Serialization;

using System.IO;

namespace EC.Permissions

{

    public class Config :

 IConfigurationSectionHandler

    {

        #region IConfigurationSectionHandler Members

        public object Create(object parent, object configContext, System.Xml.XmlNode section)

        {

            //EC.Permissions.PermissionInfo

            string typeName = ((XmlElement)section).GetAttribute("type");

            Type type = Type.GetType(typeName);

            XmlSerializer xz = new XmlSerializer(type);

            using (StringReader sr = new StringReader(section.OuterXml))

            {

                return xz.Deserialize(sr);

            }

        }

        #endregion

    }

}

    2.EC.Permissions.PermissionInfo类  

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Serialization;

namespace EC.Permissions

{

    [Serializable]

    [XmlRoot("Permission")]

    public class PermissionInfo

    {

        /// 

        /// 权限数据库ID

        /// 

        public int PermissionId { get; set; }

        /// 

        /// 用户组ID

        /// 

        public string RoleId { get; set; }

        /// 

        /// 用户组名

        /// 

        public string RoleName { get; set; }

        /// 

        /// 模块路径

        /// 

        [XmlElement("ModulePath", typeof(string))]

        public string ModulePath { get; set; }

        /// 

        /// 模块名字

        /// 

        [XmlElement("ModuleName", typeof(string))]

        public string ModuleName { get; set; }

        /// 

        /// 权限值

        /// 

        public PermissionType PermissionValue { get; set; }

        [XmlArrayItem("Item", typeof(PermissionItemInfo))]

        public PermissionItemInfo[] Items { get; set; }

    }

    [Serializable]

    [XmlRoot("Items")]

    public class PermissionItemInfo

    {

        [XmlElement("Page", typeof(string))]

        public string Page { get; set; }

        [XmlElement("Function", typeof(string))]

        public string Function { get; set; }

        [XmlElement("Val", typeof(int))]

        public int Value { get; set; }

        public override bool Equals(object obj)

        {

            if (obj == null)

                return false;

            if (this.GetType() !

= obj.GetType())

                return false;

            if (((PermissionItemInfo)obj).Page == this.Page) return true;

            return base.Equals(obj);

        }

    }

    /// 

    /// 权限类型

    /// 

    public enum PermissionType

    {

        ALL=0,

        LIST=2,

        VIEW=4,

        ADD=8,

        EDIT=16,

        DELETE=32,

        PRINT=64

    }

}

   3.EC.Permissions.DAL数据库访问  

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

using System.Web.Security;

using System.Data;

using EC.DBUtility;

using System.Data.SqlClient;

using System.Configuration;

using System.Web;

namespace EC.Permissions

{

    public class DAL

    {

        private static DataAccessManager dataAccessManager = new DataAccessManager();

        /// 

        /// 判断页面是否有此权限

        /// 1.得到当前用户所有的用户组

        /// 2.得到所有用户组的权限并取并集

        /// 

        /// 

        /// 

        public static bool CheckPage()

        {

            bool bResult = false;

            PermissionInfo pi = GetPermissionInfo;

            if (pi == null)

                return true;

            Hashtable ht = GetPermissionsByRole();

            if (ht.Count > 0)

            {

                string key = string.Format("M-{0}", FolderPath);

                if (ht.ContainsKey(key))

                {

                    string filename = GetFileName;

                    //拆箱取出并判断权限

                    PermissionInfo ht_PermissionInfo = (PermissionInfo)ht[key];

                    foreach (PermissionItemInfo temp in pi.Items)

                    {

                        if (temp.Page.ToLower().Equals(filename) && ((Convert.ToInt32(ht_PermissionInfo.PermissionValue)&temp.Value)==temp.Value))

                        {

                            bResult = true;

                            break;

                        }

                    }

                }

            }

            return bResult;

        }

        /// 

        /// 判断按钮是否有此权限

        /// 1.得到当前用户所有的用户组

        /// 2.得到所有用户组的权限并取并集

        /// 

        /// 

        /// 

        public static bool CheckButton(PermissionType type)

        {

            bool bResult = false;

            Hashtable ht = GetPermissionsByRole();

            if (ht.Count > 0)

            {

                string key = string.Format("M-{0}", FolderPath);

                if (ht.ContainsKey(key))

                {

                    //拆箱取出并判断权限

                    PermissionInfo ht_PermissionInfo = (PermissionInfo)ht[key];

                    bResult = (ht_PermissionInfo.PermissionValue & type) == type;

                }

            }

            return bResult;

        }

        

        /// 

        /// 当前目录

        /// 

        public static String FolderPath

        {

            get

            {

                string paths = HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString();

                return paths.ToLower();

            }

        }

        /// 

        /// 获取当前访问页面地址

        /// 

        public static string GetFileName

        {

            get

            {

                string paths= HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString();

                return paths.Substring(paths.LastIndexOf("/") + 1).ToLower();

            }

        }

        /// 

        /// 获取当前目录下权限配置集合

        /// 

        public static PermissionInfo GetPermissionInfo

        {

            get

            {

                return (PermissionInfo)ConfigurationManager.GetSection("Permission");

            }

        }

        /// 

        /// 得到所有用户组的权限并取并集

        /// 

        /// 

        public static Hashtable GetPermissionsByRole()

        {

            Hashtable ht = new Hashtable();

            string[] rolesArr=Roles.GetRolesForUser();

            string roles = string.Join("','", rolesArr);

            roles = "'" + roles + "'";

            SqlParameter[] parms = new SqlParameter[2]; 

            parms[0] = new SqlParameter("roles", SqlDbType.NVarChar);

            parms[0].Value = roles;

            parms[1] = new SqlParameter("path", SqlDbType.NVarChar);

            parms[1].Value = FolderPath;

            //读取用户组的所有权限

            using (IDataReader dr = dataAccessManager.ReadDatabase.ExecuteSpReader("aspnet_Ex_GetPermissionByRoles", parms))

            {

                while (dr.Read())

                {

                    PermissionInfo permissioninfo = new PermissionInfo();

                    permissioninfo.PermissionId = Convert.ToInt32(dr["PermissionId"]);

                    permissioninfo.RoleId = Convert.ToString(dr["RoleId"]);

                    permissioninfo.ModulePath = Convert.ToString(dr["ModulePath

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2