Teng_s2000的专栏

越来越懒了啊什么也不愿意干了!

用户操作
[即时聊天] [发私信] [加为好友]
滕帅ID:Teng_s2000
255973次访问,排名238好友23人,关注者69
爱好广泛,但是最近心比较烦什么也不想干!
Teng_s2000的文章
原创 263 篇
翻译 0 篇
转载 93 篇
评论 74 篇
滕帅的公告
本blog上的部分文章来自,主要用于学习与收藏,并无商业目的。如果涉及版权问题,请通知,我会尽快删掉!
联系方式:
QQ:37210956(白天不在线)
MSN:teng_s2000@126.com
地点:北京朝阳区
手机:暂时保密
我的博客:http://tengs2000.cnblogs.com

最近评论
Amway1141:很不错的东西 值的收藏 CODE 谢谢 你的分享
liuluo520:收藏
谢谢
lushipeng96:终于把“imginit”那个错误去掉了!把imgInit替换成document.all["imgInit"]就可以了!下面是正确的代码:


<script language="javascript" type="text/javascript" >

var imgWidth=260; /……
lushipeng96:有imgInit的错误,要把form标签去掉,不行啊!像这种图片轮播大多在首页以用户控件的形式出现,首页98%的都会有form标签的,没有form标签怎样运行.net程序勒!

  哎~继续调度js代码!
Teng_s2000:楼上的可以按照我的步骤重新来一遍吧,我的代码确实是调试通过的,CSDN不能上传附件!所以没有办法把代码弄上来了
文章分类
收藏
    相册
    这就是我
    博客收藏
    友情链接
    周民
    李洪彦
    杨越
    存档
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 asp.net中forms验证收藏

    新一篇: asp.net2.0中Membership的扩展 | 旧一篇: web页面中按ESC关闭窗口

    asp.net中的forms验证

    1.    <authentication mode="Forms" >

          <forms defaultUrl="~/login.aspx" loginUrl="~/login.aspx"  name=".dyj" protection="All" requireSSL="false" >

          </forms>

    </authentication>

    如果forms验证,下面的配置必不可少

    <authorization>

             <deny users="?"/>

             <!--如果使用forms验证没有这个,不会起作用的。禁止匿名访问-->

    </authorization>

    注意:denyallow是按照从上而下的顺序来执行的。

    2.通过设置与<system.web>同级的location可以设置用户访问某个文件夹的权限,详细见web.config的代码,例如:

    <location path="Admin">

          <!--Admin需要受限制的文件夹-->

          <system.web>

             <authorization>

                <allow users="admin"/>

                <!--admin就是登录框中要验证的用户名-->

                <deny users="*"/>

             </authorization>

          </system.web>

       </location>

    3.下面就可以在web页面中编写代码了

       /// <summary>

        /// 从数据库中获取用户名和密码进行验证

        /// </summary>

        /// <param name="userName"></param>

        /// <param name="passWord"></param>

        /// <returns></returns>

        private bool ValidateUser(string userName, string passWord)

        {

           using( SqlConnection connection=new SqlConnection(@"Data Source=(local);Initial Catalog=northwind;Integrated Security=True"))

           {

               connection.Open();

               SqlCommand command=new SqlCommand("select count(*) from userinfo where userid=@user and password=@password",connection);

     

               SqlParameter user = new SqlParameter("@user", userName);

               command.Parameters.Add(user);

               SqlParameter pwd = new SqlParameter("@password", passWord);

               command.Parameters.Add(pwd);

     

               int i = Convert.ToInt32(command.ExecuteScalar());//返回首行首列

               if (i == 1)

               {

                   return true;

               }

               else

               {

                   Response.Write("<script>alert('用户名或者密码错误请核实')</script>");

     

                   return false;

               }

           }

    }

     

        /// <summary>

        /// 登录

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (ValidateUser(TextBox1.Text.Trim(),TextBox2.Text.Trim()))

            {

                if (Request["ReturnUrl"] == null || Request["ReturnUrl"] == "")

                {

                    //必须指定验证通过后跳转的页面

                    FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);

                    if (TextBox1.Text.Trim() == "admin")

                    {

                        Response.Redirect("~/admin/default.aspx");

                    }

                    else

                    {

                        Response.Redirect("Index.aspx");

                    }

                }

                else

                {

                    //返回到先前未登录的页面,加入票证

                                }

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,

            TextBox1.Text,//用户名

            DateTime.Now,//票证发出时本地的日期和时间

            DateTime.Now.AddMinutes(30),//票证过期时本地的日期和时间

            true,//票证是否储存在持久性cookie

            "userData",//储存在票证中用户的特定数据

            FormsAuthentication.FormsCookiePath//票证储存在cookie中的路径

            );

                    // 加密票证

                    string encTicket = FormsAuthentication.Encrypt(ticket);

                    //创建cookie

                    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

                    FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, true);

     

            }

    }

        /// <summary>

        /// 登出

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void Button2_Click(object sender, EventArgs e)

        {

            FormsAuthentication.SignOut();

            Response.Redirect("~/default.aspx");

    }

     

    代码全部调试成功了。环境:vs2005.上面就是我编写的所有的页面代码。下面把web.config完整的贴出来。

    <?xml version="1.0"?>

    <configuration>

       <appSettings/>

       <connectionStrings/>

       <system.web>

          <compilation debug="true"/>

          <authentication mode="Forms">

             <forms defaultUrl="~/Default.aspx" loginUrl="~/default.aspx" name=".dyj" protection="All">

                <!--defaultUrl默认页面,loginUrl登录页面,namecookie名称,protectioncookie加密-->

             </forms>

          </authentication>

          <authorization>

             <deny users="?"/>

             <!--如果使用forms验证没有这个,不会起作用的。禁止匿名访问-->

          </authorization>

       </system.web>

       <location path="Admin">

          <!--Admin需要受限制的文件夹-->

          <system.web>

             <authorization>

                <allow users="admin"/>

                <!--admin就是登录框中要验证的用户名-->

                <deny users="*"/>

             </authorization>

          </system.web>

       </location>

       <location