Session全局登录验证
来源:互联网 发布:日本女生保养知乎 编辑:程序博客网 时间:2024/05/29 07:34
1.创建一个类
CheckReght.cs 放置App_Code下
实现 IHttpModule 接口
代码:
using System;
using System.Data;
using System.Configuration;
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.Xml.Linq;
/// <summary>
///CheckRight 的摘要说明
/// </summary>
public class CheckRight:IHttpModule
{
public CheckRight()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region IHttpModule 成员
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
//验证Session权限
void context_AcquireRequestState(object sender, EventArgs e)
{
//验证Session权限
HttpApplication app = sender as HttpApplication;
HttpContext content = app.Context;
//如果请求地址不是登录页面 才判断权限
if (!content.Request.RawUrl.ToLower().Contains("login.aspx")&&!content.Request.RawUrl.ToLower().Contains(".jpg"))
{
if (content.Session["user"]==null)
{
content.Response.Redirect("~/Login.aspx?returnurl=" + content.Request.RawUrl);
}
}
}
#endregion
}
2.web.config 下面注册
<system.web>下面的
<httpModules>
<add name="right" type="CheckRight"/>
</httpModules>
</system.web>
3.登录button按钮下的代码
if (txtName.Text.Trim()=="admin"&&txtPwd.Text.Trim()=="admin")
{
Session["user"] = txtName.Text.Trim();
if (string.IsNullOrEmpty(Request.QueryString["returnurl"]))
{
Response.Redirect("Default.aspx");
}
else
{
Response.Redirect(Request.QueryString["returnurl"]);
}
}
else
{
Response.Write("登录失败");
}
- Session全局登录验证
- 登录验证全局控制的几种方式(session)
- 使用Session验证用户登录
- Dictionary代替session验证登录
- php session 登录退出验证
- PHP登录session验证实例
- 使用Session验证用户登录
- Session实现验证码登录
- Express + Session 实现登录验证
- cookie session 和登录验证
- CI框架全局登录验证控制方法
- MVC全局登录验证——Action全局过滤器
- php session登录验证实例与 cookie登录验证实例
- php session应用实例--登录验证
- php session应用实例--登录验证
- php session应用实例 登录验证
- Servlet通过session进行登录验证
- session 学习-为登录添加验证码
- 关于C#的精确计时问题
- oracle 分析函数
- maven-resources-plugin prior to 2.4 is not supported by m2e
- 107. Cry for the moon. 海底捞月
- hibernate 4
- Session全局登录验证
- 使用浏览器兼容的方式创建 异步对象
- HTTP 状态代码的完整列表
- DbHelperSQL.cs--邹老师总结
- oom-kill分析
- ubuntu11.10下 qt Creator 无法在模块路径中找到主题引擎:“pixmap” 以及 Gtk-WARNING **: 无法在模块路径中找到主题引擎:“pixmap”?
- 用vector保存对象时保存指针的优点, 以及reserve的使用
- Visited的小小总结
- oom-kill分析