C#自定义用户浏览网页权限

来源:互联网 发布:unity3d 5.x下载 编辑:程序博客网 时间:2024/05/21 09:53

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;


/// <summary>
///PageBase 的摘要说明
/// </summary>
public class AdminPageBase:System.Web.UI.Page
{
    //查找用户类型
    public int UserType()
    {
        SqlConnection cn = conn.CreateConnection();
        cn.Open();
        SqlCommand cmd = new SqlCommand("select [UserType] from [Members] where [UserName]='" + Session["UserName"].ToString() + "' and [Password]='" + Session["PassWord"].ToString() + "'", cn);
        int i = Convert.ToInt32(cmd.ExecuteScalar());
        return i;

    }
    protected override void OnInit(EventArgs e)
   {
        base.OnInit(e);
        this.Load += new System.EventHandler(PageBase_Load);
        this.Error += new System.EventHandler(PageBase_Error);

    }
    //错误处理
    protected void PageBase_Error(object sender, System.EventArgs e)
    {
        string errMsg = string.Empty;
        Exception currentError = HttpContext.Current.Server.GetLastError();
        errMsg += "<h1>系统错误:</h1><hr/>系统发生错误, " +
        "该信息已被系统记录,请稍后重试或与管理员联系。<br/>" +
        "错误地址: " + Request.Url.ToString() + "<br/>" +
        "错误信息: " + currentError.Message.ToString() + "<hr/>" +
        "<b>Stack Trace:</b><br/>" + currentError.ToString();
        HttpContext.Current.Response.Write(errMsg);
        Server.ClearError();
     }
    private void PageBase_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            {
                if (HttpContext.Current.Session["UserName"] != null && HttpContext.Current.Session["PassWord"] != null)
                    {
                        if (UserType()<3)
                        {
                            HttpContext.Current.Response.Write("<script> alert('您不是管理员,没有访问权限!');location='/resdc/Default.aspx'</script>");
                        }
                    }
                else
                    {
                        HttpContext.Current.Response.Write("<script> alert('您未登录,目前没有权限访问!');location='/resdc/Default.aspx'</script>");
                    }
             }
      }
  
}

网页页面中调用方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Default : AdminPageBase   //把继承接口System.Web.UI.Page改为AdminPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}