利用Forms实现两种不同验证系统
2007-06-25 15:55
405 查看
<script type="text/javascript"><!--
google_ad_client = "pub-2492996434662062";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
//2007-10-23: csdn上的广告
google_ad_channel = "3993411652";
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
经常看到的Forms验证都是在一个应用程序下实现一种验证登陆系统。而我要做的项目,要在一个应用程序下实现两种不同的登陆验证方式(前台登陆和后台登陆)。费了一些功夫终于做出来了。其实实现方法很简单,我把主要代码贴出来希望大虾们不要笑话。
与别的Forms验证相同的部分我不再写了,可参照我的上一篇:Froms验证 或到网上艘 到处都是。
<authentication mode="Forms">
<forms name=".ASPXFORMSDEMO" loginUrl="LoginRedirect.aspx" protection="All" path="/"
timeout="30" />
</authentication>
注意loginUrl并不是真正的登陆页面而是一个重定向页面,由他判断是跳转到哪一个登陆页面。代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace TestForms
...{
/**//**//**//// <summary>
/// LoginRedrirect 的摘要说明。
/// </summary>
public class LoginRedirect : System.Web.UI.Page
...{
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
string url=Request.QueryString["ReturnUrl"];
if(url != null )
...{
if(Request.ApplicationPath !="/")
...{
url=url.Replace(Request.ApplicationPath,"");
}
if(!url.StartsWith("/"))
url="/"+url;
string path=url.Split('/')[1].ToLower();
switch (path)
...{
case "admin":
Response.Redirect("admin/AdminLogin.aspx?ReturnUrl="+Server.HtmlDecode(Request.QueryString["ReturnUrl"]),false);
break;
case "user":
Response.Redirect("user/UserLogin.aspx?ReturnUrl="+Request.QueryString["ReturnUrl"],false);
break;
default :
Response.Redirect("#");
break;
}
}
else
Response.Redirect("#");
}
Web 窗体设计器生成的代码Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//**//**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
在Web.config中添加
<location path="user">
<system.web>
<authorization>
<allow roles="user"></allow>
<deny users="*" /> <!-- 只允许验证用户访问 -->
</authorization>
</system.web>
</location>
<location path="admin">
<system.web>
<authorization>
<allow roles="Admin"></allow>
<deny users="*" /> <!-- 只允许验证用户访问 -->
</authorization>
</system.web>
</location>
<location path="admin/AdminLogin.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="user/UserLogin.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
注意:在登陆的时候还要添加组 (Admin 或 user)并且管理文件都在Admin目录中前待续验证文件都在user中
以此类推可在一个应用程序下实现多种验证登陆系统
google_ad_client = "pub-2492996434662062";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
//2007-10-23: csdn上的广告
google_ad_channel = "3993411652";
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
经常看到的Forms验证都是在一个应用程序下实现一种验证登陆系统。而我要做的项目,要在一个应用程序下实现两种不同的登陆验证方式(前台登陆和后台登陆)。费了一些功夫终于做出来了。其实实现方法很简单,我把主要代码贴出来希望大虾们不要笑话。
与别的Forms验证相同的部分我不再写了,可参照我的上一篇:Froms验证 或到网上艘 到处都是。
<authentication mode="Forms">
<forms name=".ASPXFORMSDEMO" loginUrl="LoginRedirect.aspx" protection="All" path="/"
timeout="30" />
</authentication>
注意loginUrl并不是真正的登陆页面而是一个重定向页面,由他判断是跳转到哪一个登陆页面。代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace TestForms
...{
/**//**//**//// <summary>
/// LoginRedrirect 的摘要说明。
/// </summary>
public class LoginRedirect : System.Web.UI.Page
...{
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
string url=Request.QueryString["ReturnUrl"];
if(url != null )
...{
if(Request.ApplicationPath !="/")
...{
url=url.Replace(Request.ApplicationPath,"");
}
if(!url.StartsWith("/"))
url="/"+url;
string path=url.Split('/')[1].ToLower();
switch (path)
...{
case "admin":
Response.Redirect("admin/AdminLogin.aspx?ReturnUrl="+Server.HtmlDecode(Request.QueryString["ReturnUrl"]),false);
break;
case "user":
Response.Redirect("user/UserLogin.aspx?ReturnUrl="+Request.QueryString["ReturnUrl"],false);
break;
default :
Response.Redirect("#");
break;
}
}
else
Response.Redirect("#");
}
Web 窗体设计器生成的代码Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//**//**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
在Web.config中添加
<location path="user">
<system.web>
<authorization>
<allow roles="user"></allow>
<deny users="*" /> <!-- 只允许验证用户访问 -->
</authorization>
</system.web>
</location>
<location path="admin">
<system.web>
<authorization>
<allow roles="Admin"></allow>
<deny users="*" /> <!-- 只允许验证用户访问 -->
</authorization>
</system.web>
</location>
<location path="admin/AdminLogin.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="user/UserLogin.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
注意:在登陆的时候还要添加组 (Admin 或 user)并且管理文件都在Admin目录中前待续验证文件都在user中
以此类推可在一个应用程序下实现多种验证登陆系统
相关文章推荐
- 利用Forms实现两种不同验证系统
- 两种利用GCD实现分步获取结果的方式和SDWebImage缓存机制的验证
- 利用两种不同的数据结构实现双端栈
- 两种利用GCD实现分步获取结果的方式和SDWebImage缓存机制的验证
- 利用SMTP虚拟服务器实现不同邮件系统之间的通讯
- MOSS与业务系统的集成 之 自定义Membership实现Forms方式验证
- 两系统用asp.net forms 身份验证方式实现跨域登录信息共享
- 利用SMTP虚拟服务器实现不同邮件系统之间的通讯
- 利用两种不同的NAT配置实现两个接口的双向静态NAT测试
- 不同系统平台之间(Linux与Linux、Linux与Unix)利用NFS实现文件共享方法
- 利用HttpModule和forms身份验证,实现角色验证控制
- spirng-boot中,基于既有的token验证方式,利用spring-security实现权限系统
- 分布式系统下利用 AOP 实现 session 身份验证的一种方案
- Android中两种不同的方法实现实时更新时间,是显示时间和系统时间同步
- 不同系统平台之间(Linux与Linux、Linux与Unix)利用NFS实现文件共享方法
- MOSS与业务系统的集成 之 自定义Membership实现Forms方式验证
- 利用ACS实现AAA验证
- [转]Android中Xposed框架篇—利用Xposed框架实现拦截系统方法
- 用ASP.Net Forms模式实现WebService身份验证
- 利用Dll实现通用密码验证框