后台页面访问权限:页面基类&内置票据认证 使用方法

一般网站后台页面除了登录页面login.aspx未登录用户可访问外,其它页面必须登录用户才可访问,

当用户未登录时进入某个页面则自动判断并跳转到登录页面:

(如果login.aspx页面用到图片及Css、Js文件,那么也必须允许未登录用户可访问Images、Css、Js文件夹)

 

方法一:运用页面基类BasePage.cs

1、BasePage.cs代码:

 

01/*
02 * 创建人:余泳彬
03 * 创建时间:2011-1-17 11:13:32
04 * 说明:页面基类
05 * 版权所有:余泳彬
06 */
07using System;
08using System.Collections.Generic;
09using System.Web;
10  
11namespace Common
12{
13    /// <summary>页面基类</summary>
14    public class BasePage : System.Web.UI.Page
15    {
16  
17        /// <summary>
18        /// 应用程序路径 如:/YongbinWeb/ 或 /
19        /// </summary>
20        public string ApplicationPath
21        {
22            get
23            {
24                string path = HttpContext.Current.Request.ApplicationPath.ToString();
25                if (path.Trim() != "/") // 判断路径是否为“/”
26                {
27                    path += "/";
28                }
29                return path;
30            }
31        }
32  
33  
34        /// <summary>
35        /// 重写页面预处理事件(在页面初始化开始时引发)
36        /// 验证用户是否登录
37        /// </summary>
38        protected override void OnPreInit(EventArgs e)
39        {
40            //判断会员是否登录,若未登录则跳转到登陆页面
41            if (Session["admin"] == null)
42            {
43                this.Response.Redirect(ApplicationPath + "/admin/login.aspx", true); 
44                return;
45            }
46            base.OnPreInit(e);
47        }
48  
49    }
50}


2、后台需设置权限的页面.aspx.cs代码(继承BasePage类即可):

1public partial class admin_ad_edit : BasePage
2{
3    protected void Page_Load(object sender, EventArgs e)
4    {
5  
6     }
7}

  

方法二:运用.Net内置票据认证

 

 

 

 

01protected void Application_AuthenticateRequest(object sender, EventArgs e)
02        {
03            // .Net 内置票据认证代码
04            if (HttpContext.Current.User != null)
05            {
06                if (HttpContext.Current.User.Identity.IsAuthenticated)  // 验证过的一般用户才能进行角色验证  
07                {
08                    if (HttpContext.Current.User.Identity is FormsIdentity)
09                    {
10                        FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
11                        FormsAuthenticationTicket tiecket = id.Ticket;  // 取得身份验证票  
12                        string userData = tiecket.UserData;    // 从UserData中恢复role信息
13                        string[] roles = userData.Split(',');       // 将角色数据转成字符串数组,得到相关的角色信息
14                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);  // 这样当前用户就拥有角色信息了
15                    }
16                }
17            }
18  
19        }


2、  web.config 文件中配置目录权限及登录页

  

登录页,在system.web节点中

1<!--
2    票据认证配置:登陆页。 
3    通过 <authentication> 节可以配置 ASP.NET 用来
4    识别进入用户的安全身份验证模式。
5-->
6<authentication mode="Forms">
7    <forms name="mycook" loginUrl="Admin/login.aspx" protection="All" path="/"/>
8</authentication>


 

配置目录权限,在system.web节点外面

  

 


3、  在登录页的登录事件中的登录成功后拷入一段代码

 

01//  登陆事件
02protected void btnLogin_Click(object sender, ImageClickEventArgs e)
03{
04    string name = txtName.Text.Trim();  // 用户名
05    string pwd = txtPassWord.Text.Trim();  // 密码
06    if (name == "yongbin" && pwd == "123456")
07    {
08        // 登录成功,内置票据认证拷入代码
09        HttpCookie cook;
10        string strReturnURL;  // 登录成功后返回的URL
11        string roles = "admin"; // 用户角色
12        // 建立身份验证票对象
13        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
14            1, name, DateTime.Now, DateTime.Now.AddMinutes(30), false, roles);
15        cook = new HttpCookie("mycook");
16        cook.Value = FormsAuthentication.Encrypt(ticket);
17        Response.Cookies.Add(cook);
18        strReturnURL = Request.Params["ReturnUrl"];
19        if (strReturnURL != null)
20        {
21            Response.Redirect(strReturnURL);
22        }
23        else
24        {
25            Response.Redirect("Default.aspx");
26        }
27  
28    }
29}

 

 

 

 

注:.Net票据认证是看了牛腩老师的《牛腩购物网》视频教程学会的,牛哥是个好人,课也讲得不错

推荐给大家:视频教程是收费的,但是非常便宜,只售¥30元,有兴趣的可以他的淘宝店购买:牛腩的淘宝网店

01<!--票据认证配置:目录访问权限-->
02<location path="Admin">
03    <system.web>
04        <authorization>
05            <allow roles="admin"/>   <!--允许指定admin角色用户可访问-->
06            <deny users="*"/>          <!-- 禁止所有非指定访问用户的访问-->
07        </authorization>
08    </system.web>
09</location>
10<!--所有用户均可访问登录页面-->
11<location path="Admin/login.aspx">
12    <system.web>
13        <authorization>
14            <allow users="*"/>
15        </authorization>
16    </system.web>
17</location>
18<!--所有用户均可访问skin文件夹(css,images文件)-->
19<location path="Admin/skin">
20    <system.web>
21        <authorization>
22            <allow users="*"/>
23        </authorization>
24    </system.web>
25</location>

1、  在根目录建立一个全局应用程序类Global.asax文件,拷入一段代码:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值