您的位置:首页 > 编程语言 > Go语言

利用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中

以此类推可在一个应用程序下实现多种验证登陆系统
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息