我是初学者求一个实例学习。要求如下。
我有个数据库test.mdb
有个login.aspx、web.config、ok.aspx
login.aspx:
两个<asp:TextBox>,分别为用户名和密码
一个<asp:button>
当按钮触发的时候,比较数据库里的用户名和密码,正确转到ok.aspx,错误提示用户名或密码错误
ok.aspx
显示欢迎信息。
web.config
配置身份验证为forms,服务器路径,数据库地址跟数据库连接。
整个实例要用asp.net(c#)写的。而且没有经过login.aspx不能直接访问如ok.aspx等文件。如果直接通过地址访问都跳转到login.aspx先登录。相信对于各位高手一定是很简单吧。写好的源码直接发到我邮箱。谢谢。血汉百拜。测试通过立即结帖给分。
[email protected]
[email protected]

解决方案 »

  1.   

    if (数据返回标识)
    {
    Response.Redirect("../member/Login.aspx");
    }
    正确转到ok.aspx不能直接访问如ok.aspx等文件http://www.microsoft.com/china/technet/security/guidance/secmod18.mspx
      

  2.   

    参考这篇文章:
    http://www.98700.com/Article/ShowArticle.asp?ArticleID=3175
      

  3.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=ATV1GLXT-65FF-4M82-CT5U-B1J65D3ZN2OK
      

  4.   

    先建数据库:(在SQL数据库的查询分析器里输入下面的语句)create table test( userCode varchar(20) PRIMARY KEY not null,userName varchar(20) not null)
      

  5.   

    用ASP.net的Forms验证呀,这方面得东西可以看看:
    http://www.cnblogs.com/caca/archive/2004/07/08/22451.html
      

  6.   

    先建数据库:(在SQL数据库的查询分析器里输入下面的语句)
    create table test( userCode varchar(20) PRIMARY KEY not null,userName varchar(20) not null,userPass varchar(20) not null)
      

  7.   

    高手们解释一下吧,偶看不懂喔.不是语法.POST发送数据有什么用啊?哪里应用??哪里用到过?
      

  8.   

    数据库建好后
    再新建项目testlogin  添加两个页面login.aspx  和  ok.aspx 然后 login.aspx放个Label1 和Label2
    Text取名  用户编号、密码   再加个按钮Button1     ok.aspx 里放个Label1修改Web.config如下:最下面的</system.web>和</configuration>之间加上这句:
     <appSettings>
    <add key="connectionstr" value="server=127.0.0.1;user id=sa;password=你的数据库密码;database=test;Max Pool Size=100;"/>
    </appSettings>然后再修改这里    <authentication mode="Forms"> 
    <forms name="testlogin " loginUrl="login.aspx" protection="All" path="/" />
    </authentication>还有这里:    <authorization>
            <deny users="?" /> 
        </authorization>
      

  9.   

    protection="All"
    是什么意思?
    还有就是使用
    FormsAuthentication.RedirectFromLoginPage
    函数是不是一定要转到default.aspx
    能不能转到自己定义的页面?
    谢谢
      

  10.   

    刚才忘说还要放两个TextBox了  分别是TextBox1和TextBox2   输入用户和密码
    然后在login.aspx的HTML里:将这行<body MS_POSITIONING="GridLayout">换成这样:
    <body MS_POSITIONING="GridLayout" onload="if(top != window)top.location.href=window.location.href;">现在开始写代码   先把login.aspx.cs里的顶部的那些using全部换成这些
    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;
    using System.Data.SqlClient;
    using System.Web.Security;再在Button1的点击事件(双击)里写如下代码:
    if((this.TextBox1.Text=="")||(this.TextBox2.Text=="")) 
    Page.RegisterStartupScript("check", "<script>alert('请正确输入用户和密码!');</script>");
    else
    {
    string connectstring = System.Configuration.ConfigurationSettings.AppSettings["connectionstr"];
    SqlConnection Conn = new SqlConnection();
    Conn.ConnectionString = connectstring;
    SqlDataAdapter Adp = new SqlDataAdapter(connectstring,Conn); 
    string strLogin = "select count(UserCode) from test where UserCode='"+this.TextBox1.Text.Trim()+"' and userPass='"+ this.TextBox2.Text.Trim() + "'";
    Adp.SelectCommand.CommandText =strLogin;
    int num = (int)Adp.SelectCommand.ExecuteScalar();
    if (num>0)
    {
    this.Session.Clear();
    this.Session["UserCode"] = TextBox1.Text;
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(this.Session["UserCode"].ToString(), true, 60);
    string EncTicket = FormsAuthentication.Encrypt(ticket);
    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, EncTicket));
    Response.Redirect("ok.aspx",false);
    }
    else
    {
    Page.RegisterStartupScript("check", "<script>alert('用户或者密码错误!');</script>");
    }
    }
      

  11.   

    在ok.aspx里的Page_Load事件里写如下代码:
    this.Label1.Text = "欢迎"+this.Session["UserCode"].ToString()+"成功登录!";最后   在数据库test里随便加几个用户 将login.aspx设置成起始页 把项目编译一下  看行不行
    不行再说 反正今天晚上我值夜班  闲着也是闲着  哈哈
      

  12.   

    忘了一点  数据库连接要先打开  在Adp.SelectCommand.CommandText =strLogin;后加上这个 Adp.SelectCommand.Connection.Close();
    Adp.SelectCommand.Connection.Open();
      

  13.   

    对不起,请各位高手不要鄙视我。我是初学者,刚学asp.net一个星期,主要是看quickstart。不是我懒,而是我学不出来,我想借你们的实例参考一下,我想开发个小型的个人网站而已,而且我是业余学的,你们把代码写在这边我只懂一点点,还是不清楚,所以高手们谢谢你们的指导。但是我让死理,我收到邮件就封帖。再次谢谢你们的帮助。
      

  14.   

    Form认证就好了,这坛子上有这方面的资料的,搜索一下吧
      

  15.   

    楼主   我帮你写的那个有说明的啊  你照着一步一步做(前提是你机器上必须要有Visual Studio.NET 2003和SQL Server 2000 当然还得装IIS 6.0和FrontPage2000扩展) 做完你不就会了嘛  从建数据库的表 到建项目  到如何在项目里写代码  以及代码该写在那里  都说得很清楚了啊 你还要.... 呵呵
      

  16.   

    snowpine999([彼岸烟花]) 谢谢你的帮助,不过我不懂SQL,所以不知道怎么做。能不能给个access的。