利用Forms实现两种不同验证系统

<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 窗体设计器生成的代码
    }

}

 在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中

以此类推可在一个应用程序下实现多种验证登陆系统
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lanlandechong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值