利用HttpModule 检查用户是否有权限打开指定的网址
2011-02-21 17:11
507 查看
1. 建立一个网站项目
2. 在app_code 里面 新建立一个类
/// <summary>
/// 说明:检查用户是否有权使用模块的Module
/// 作者:郑文亮
/// 联系:http://www.cnblogs.com/zhwl
/// </summary>
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
public class SystemModuleAuthorizationModule : IHttpModule
{
#region IHttpModule 成员
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
void context_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
string d = application.Request.Url.ToString();
// 如果用户未登录,则无需检查模块授权,因为请求会被用户登录Module重定向到登录页面。
if (application.Session["UserName"] == null)
{
string requestUrl = application.Request.Url.ToString();
string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1);
// 如果请求的页面不是登录页面,刚重定向到登录页面。
if (requestPage != "Login.aspx")
application.Server.Transfer("Login.aspx");
return;
}
// 获取用户名和Url
string userName = application.Session["UserName"].ToString();
string url = application.Request.Url.ToString();
// 如果用户没有被授权,请求被终止,并打印提示信息。
if (!Validator.CanUseModule(userName, url))
{
application.CompleteRequest();
application.Response.Clear();
application.Response.Write(string.Format("对不起!{0},您无权访问此模块!", userName));
}
else
{
application.Response.Write(string.Format("欢迎您!{0}!", userName));
}
}
#endregion
}
public class Validator
{
/// <summary>
/// 检查用户是否被授权使用模块。
/// aaa可以使用模块 a.aspx, 其他的情况返回false
/// </summary>
/// <param name="userName"></param>
/// <param name="url"></param>
/// <returns></returns>
public static bool CanUseModule(string userName, string url)
{
if (url.Contains("login.aspx") == true)
{
return true;
}
if (userName == "aaa" && url.Contains("a.aspx"))
{
return true;
}
else
{
return false;
}
}
}
3. web.config 文件 配置如下
<httpModules>
<add name="SystemModuleAuthorizationModule" type="SystemModuleAuthorizationModule" />
</httpModules>
备注: 因为类文件放在app_Code 下面 所以没有加命名空间的名称(不需要)
如果是一个wen应用程序
<system.web>
<httpModules>
<add name="SystemModuleAuthorizationModule" type="WebApplication3.SystemModuleAuthorizationModule" />
</httpModules>
</system.web>
注解: WebApplication3 是这个项目的命名空间
4. 建立login.aspx 里面有一个按钮
后台文件:
protected void Button1_Click(object sender, EventArgs e)
{
Session["UserName"] = TextBox1.Text.Trim();
}
前台页面 :
<div>
<a href="a.aspx">页面a</a>
</div>
<div>
<a href="b.aspx">页面b</a>
</div>
5. 建立一个空的a.aspx 和 b.aspx (测试用)
2. 在app_code 里面 新建立一个类
/// <summary>
/// 说明:检查用户是否有权使用模块的Module
/// 作者:郑文亮
/// 联系:http://www.cnblogs.com/zhwl
/// </summary>
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
public class SystemModuleAuthorizationModule : IHttpModule
{
#region IHttpModule 成员
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
}
void context_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
string d = application.Request.Url.ToString();
// 如果用户未登录,则无需检查模块授权,因为请求会被用户登录Module重定向到登录页面。
if (application.Session["UserName"] == null)
{
string requestUrl = application.Request.Url.ToString();
string requestPage = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1);
// 如果请求的页面不是登录页面,刚重定向到登录页面。
if (requestPage != "Login.aspx")
application.Server.Transfer("Login.aspx");
return;
}
// 获取用户名和Url
string userName = application.Session["UserName"].ToString();
string url = application.Request.Url.ToString();
// 如果用户没有被授权,请求被终止,并打印提示信息。
if (!Validator.CanUseModule(userName, url))
{
application.CompleteRequest();
application.Response.Clear();
application.Response.Write(string.Format("对不起!{0},您无权访问此模块!", userName));
}
else
{
application.Response.Write(string.Format("欢迎您!{0}!", userName));
}
}
#endregion
}
public class Validator
{
/// <summary>
/// 检查用户是否被授权使用模块。
/// aaa可以使用模块 a.aspx, 其他的情况返回false
/// </summary>
/// <param name="userName"></param>
/// <param name="url"></param>
/// <returns></returns>
public static bool CanUseModule(string userName, string url)
{
if (url.Contains("login.aspx") == true)
{
return true;
}
if (userName == "aaa" && url.Contains("a.aspx"))
{
return true;
}
else
{
return false;
}
}
}
3. web.config 文件 配置如下
<httpModules>
<add name="SystemModuleAuthorizationModule" type="SystemModuleAuthorizationModule" />
</httpModules>
备注: 因为类文件放在app_Code 下面 所以没有加命名空间的名称(不需要)
如果是一个wen应用程序
<system.web>
<httpModules>
<add name="SystemModuleAuthorizationModule" type="WebApplication3.SystemModuleAuthorizationModule" />
</httpModules>
</system.web>
注解: WebApplication3 是这个项目的命名空间
4. 建立login.aspx 里面有一个按钮
后台文件:
protected void Button1_Click(object sender, EventArgs e)
{
Session["UserName"] = TextBox1.Text.Trim();
}
前台页面 :
<div>
<a href="a.aspx">页面a</a>
</div>
<div>
<a href="b.aspx">页面b</a>
</div>
5. 建立一个空的a.aspx 和 b.aspx (测试用)
相关文章推荐
- 利用HttpModule 检查用户是否有权限打开指定的网址
- 4.2用户登陆和安全验证(利用HttpModule集中处理身份检查)
- 在系统中检测指定的窗口是否已经打开,检查某进程是否存在
- “打开ftp服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹"
- 如何检查某个用户是否具有某个权限对象上定义的某种权限
- 如何检查某个用户是否具有某个权限对象上定义的某种权限
- 【JavaScript】利用正则表达式检查输入框输入的是否为网址
- Android 检查用户权限是否开启
- 检查当前用户是否具有管理员权限
- 利用JavaScript检查用户注册信息是否正确,在以下情况不满足时报错并阻止提交表单
- windows server 打开 FTP 服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。
- (转)打开FTP服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹
- [原创]java WEB学习笔记45:自定义HttpFilter类,理解多个Filter 代码的执行顺序,Filterdemo:禁用浏览器缓存的Filter,字符编码的Filter,检查用户是否登陆过的Filter
- 利用HttpModule进行权限设置
- 关于serv-u公网访问出现“打开ftp服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。详细信息:操作超时”的解决方法
- 打开ftp服务器上的文件时发生错误。请检查是否有权限访问该文件夹
- 打开FTP服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹
- 利用正则表达式检查网址,手机号,邮箱是否正确(RegexKitLite)
- 尚未配置为Web项目.指定的本地IIS URL http://localhsst/..要打开项目,需要配置虚拟目录 。是否立即创建虚拟目录 解决
- 利用IRequiresSessionState和IReadOnlySessionState接口判断HttpContext是否具有会话读写权限