zoukankan      html  css  js  c++  java
  • 一步步打造基于ASP.NET的CMS内容管理系统Step4 权限设定(补充)

    Dotnetcms Portal基本上是我在cnblogs文章的一个整合,DEMO演示地址 

    Demo: http://portal.dotnetcms.org

     本文将介绍如何使用权限。

    关于权限,网上有很多了,不过,Dotnetcms的权限控制方式主要采用配置方式,也就是精确到页面,在

    web.config里,可以定义

     <authorization>
          <allow roles="administrators;users" />
            <deny users="*"></deny>
          </authorization>

    类似这样的访问方式,那么是怎么实现的呢?.NET本身是不支持allow这样角色的,为此我们需要实现IPrincipal接口(好像MSDN上有文章介绍)

    public class CustomPrincipal : IPrincipal
    {

    private IIdentity _identity;
    private string[] _roles;


    public CustomPrincipal(IIdentity identity, string[] roles)
    {
    _identity
    = identity;
    _roles
    = new string[roles.Length];
    roles.CopyTo(_roles,
    0);
    Array.Sort(_roles);
    }



    // IPrincipal Implementation
    public bool IsInRole(string role)
    {
    return Array.BinarySearch(_roles, role.ToLower()) >= 0 ? true : false;
    }


    public IIdentity Identity
    {
    get
    {
    return _identity;
    }
    }



    public bool IsInAllRoles( params string [] roles )
    {
    foreach (string searchrole in roles )
    {
    if (Array.BinarySearch(_roles, searchrole.ToLower()) < 0 )
    return false;
    }
    return true;
    }



    public bool IsInAnyRoles( params string [] roles )
    {
    foreach (string searchrole in roles )
    {
    if (Array.BinarySearch(_roles, searchrole.ToLower()) >= 0)
    return true;
    }
    return false;
    }

    /// <summary>
    /// 角色列表,多个角色以分号分隔
    /// </summary>
    /// <param name="roles"></param>
    /// <returns></returns>
    public bool IsInAnyRoles(string roles)
    {
    return IsInAnyRoles(roles.Split(new char[] { ';' }));
    }


    }

    然后在登录页面里,实现身份验证票,也就是把roles存到cookie里,这样就可以了 

    string Roles = "roles";
    FormsAuthenticationTicket ticket
    = new FormsAuthenticationTicket(1,uid.ToString(),DateTime.Now,DateTime.Now.AddHours(2), true,Roles);
    string encryptedTicket = FormsAuthentication.Encrypt(ticket);
    HttpCookie authCookie
    = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);
    authCookie.Path
    = FormsAuthentication.FormsCookiePath;
    authCookie.Expires
    = DateTime.Now.AddHours(2);
    Response.Cookies.Add(authCookie);

      这样还可以利用Users.IsUserInRoles("adminstrator");判断用户是否在角色里

     注意:这里仅是权限管理的一种方式,事实上,完全可以不适合任何角色,直接在Page_Load里检测用户的权限例如

    SQL="select * from roles where username='xx'"

    直接读取角色,然后对roles进行处理,他的好处简单,但是缺点:

    1)无法充分利用.NET提供的便利功能
    2)每次判断都需要到数据库里读roles,显然没有.NET提供的读取coookie快

    当然,我们也可以自定义吧roles存到cookie,但是还需要对cookie进行一些处理也是比较麻烦

    ----------------------------------------

    以下是广告时间: 

     经过两周的努力,Dotnetcms Portal系统终于上线了,
    下载地址为:
    http://www.dotnetcms.org/bbs/showforum-2.aspx

    使用指南:新手必看,否则不知道如何使用本系统  http://www.dotnetcms.org/userguid/use1.html

    Demo: http://portal.dotnetcms.org

  • 相关阅读:
    【数据结构第二周】队列知识点整理
    【数据结构第二周】堆栈知识点整理
    【数据结构第二周】线性表知识点整理
    【数据结构第一周】最大子列和问题整理
    网络设置
    QT 安装教程
    C# 复制粘贴板 多行粘贴
    设置网络适配器IP优先级
    MySQL 查重复单号和删重复单号
    Mysql 10053 SocketException 你的主机中的软件中止了一个已建立的连接。
  • 原文地址:https://www.cnblogs.com/mqingqing123/p/1747358.html
Copyright © 2011-2022 走看看