博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2005-09-03 02:16    阅读(2051)  评论(6编辑  收藏  举报
经常看到的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(
"#");
        }


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

以此类推可在一个应用程序下实现多种验证登陆系统