我是初学者求一个实例学习。要求如下。
我有个数据库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]
我有个数据库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]
解决方案 »
- asp.net中,怎样在后置代码页面(.aspx.cs页面)获取所有的Button?
- asp.net 怎样制作爬虫程序的思路及步骤?.
- **急急**请哪位大侠求救**一个水晶报表项目问题
- 请问如何在Page.Load事件中知道是哪个控件造成的回发(PostBack)事件~~~
- 网页逐屏显示
- 比较难,帮我看一下程序中出现的Oracle错误,谢谢
- 就要失业了,散分!
- C#如何判断一个网络路径是不是有效的图片路径?在线等 ...
- web控件checkboxlist好像不能用RequiredFieldValidator验证,为什么???
- 一个很好玩的问题!
- 请问如何用在datagrid中动态写入多条数据?
- 2002和2003有什么区别啊
{
Response.Redirect("../member/Login.aspx");
}
正确转到ok.aspx不能直接访问如ok.aspx等文件http://www.microsoft.com/china/technet/security/guidance/secmod18.mspx
http://www.98700.com/Article/ShowArticle.asp?ArticleID=3175
http://www.cnblogs.com/caca/archive/2004/07/08/22451.html
create table test( userCode varchar(20) PRIMARY KEY not null,userName varchar(20) not null,userPass varchar(20) not null)
再新建项目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>
是什么意思?
还有就是使用
FormsAuthentication.RedirectFromLoginPage
函数是不是一定要转到default.aspx
能不能转到自己定义的页面?
谢谢
然后在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>");
}
}
this.Label1.Text = "欢迎"+this.Session["UserCode"].ToString()+"成功登录!";最后 在数据库test里随便加几个用户 将login.aspx设置成起始页 把项目编译一下 看行不行
不行再说 反正今天晚上我值夜班 闲着也是闲着 哈哈
Adp.SelectCommand.Connection.Open();