用户登录代码

一 。这个是.net asp。cs制作

名人名言:在我们讲的一切中,我只是探求真理,这并不是仅仅为了博得说出真理的荣誉,而是因为真理于人有益。——爱尔维修

这周末也没干啥,真正开端ASP,做了个学籍经管体系的登录界面,登录界面首要包含用户名、暗码、验证码,界面字体用了改变字体,生成验证码控件 ImageButton (例如:) 还学会了图片按钮 HyperLink (例如: )此中我感觉很难的就是验证码的生成,你起首在站点中添加pivture.aspx 的窗体,打开代码页面(即pivture.aspx.cs)并录入下面代码(完全的) Code using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Drawing; using System.IO; public partial class Picture : System.Web.UI.Page { Random ran = new Random(); protected void Page_Load(object sender, EventArgs e) { string str = getRandomValidate(4); Session["CheckCode"] = str;//这一部是Wie了验证码写入Session,进行验证,也可以应用cookie getImageValidate(str); } //获得随机字符串,长度自定义 private string getRandomValidate(int len) { int num; int tem; string rtuStr=""; for (int i = 0; i < len;i++ ) { num = ran.Next(); tem = num % 10 + ""0"";//生成数字 //tem = num % 26 + ""A"";//生成字符 rtuStr += Convert.ToChar(tem).ToString(); } return rtuStr; } //生成图像 private void getImageValidate(string strValue) { //string str=oo00;前两个为字母o,后两个数为0 int width = Convert.ToInt32(strValue.Length*12); Bitmap img = new Bitmap(width,23); Graphics gfc = Graphics.FromImage(img); gfc.Clear(Color.White); drawLine(gfc,img); //写验证码,要定义Font Font font = new Font("arial",12,FontStyle.Bold); //Font font = new Font("宋体",12,FontStyle.Bold|FontStyle.Italic); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,img.Width,img.Height),Color.DarkOrchid,Color.Blue,1.5f,true); gfc.DrawString(strValue,font, brush ,3,2); drawPoint(img); gfc.DrawRectangle(new Pen(Color.DarkBlue),0,0,img.Width-1,img.Height-1); //将图像添加到页面 MemoryStream ms = new MemoryStream(); img.Save(ms,System.Drawing.Imaging.ImageFormat.Gif); //更改HTTP Response.ClearContent(); Response.ContentType = "image/gif"; Response.BinaryWrite(ms.ToArray()); //Dispose gfc.Dispose(); img.Dispose(); Response.End(); } private void drawLine(Graphics gfc,Bitmap img) { //选择画10条线,也可以增长,也可以不要线,只要随机杂点就行 for (int

i = 0; i < 10;i++ ) { int x1 = ran.Next(img.Width); int y1 = ran.Next(img.Height); int x2 = ran.Next(img.Width); int y2 = ran.Next(img.Height); gfc.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);//重视画笔要淡,不然看不清 } } //private void drawPoint(Bitmap img) //{ //} private void drawPoint(Bitmap img) { int col = ran.Next();//在一次的图片中杂点色彩雷同 for (int i = 0; i < 100; i++) { int x = ran.Next(img.Width); int y = ran.Next(img.Height); img.SetPixel(x,y,Color.FromArgb(col)); } } } 录完后,须要在 登录界面显示验证码,则须要在登录界面的代码文件 Page_Load 中参加 ImageButton1.ImageUrl = "~/Picture.aspx"; 其登岸页面的代码Default.aspx.cs中的代码为 Code using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ImageButton1.ImageUrl = "~/Picture.aspx"; if (Session["CheckCode"] == null) Information.Text = "对不起,验证码生成错误!"; } protected void OK_Click(object sender, EventArgs e) { if (UserName.Text.Length == 0) Information.Text = "请输入用户名!"; else { if(Password.Text.Length ==0) Information.Text = "请输入暗码!"; else if (CheckCode.Text.ToString() != Session["CheckCode"].ToString()) { Information.Text = "对不起,验证码不正确,请从头输入!"; } else if (UserName.Text == "Admin" && Password.Text == "Admin" && CheckCode.Text.ToString() == Session["CheckCode"].ToString()) { Information.Text = "用户:" + UserName.Text + "登录成功!"; Response.Redirect("~/Default2.aspx"); } else Information.Text = "用户不存在或暗码不正确!"; } } protected void Cancel_Click(object sender, EventArgs e) { Information.Text = "已退出登录!" ; } }


二。这个是asp页面(脚本)
ASP用户登录模块的设计.

用户登录验证脚本,Chkpwd.asp

1.<%
2.'=======用户登录验证脚本=======
3.'如果尚未定义Passed对象,则将其定义为false,表示没有通过验证
4.If IsEmpty(Session("Passed")) Then
5.Session("Passed")=false
6.End If
7.
8.'Session("Passed")=False,表示尚未通过验证,则开始读取从表单传来的验证信息
9.If Session("Passed")=False Then
https://www.360docs.net/doc/527608331.html,erName=Request.Form("UserName")
https://www.360docs.net/doc/527608331.html,erPwd=Request.Form("UserPwd")
12.
13.If UserName="" Then
14.Errmsg="提示:请输入用户名和密码"
15.Else
16.'===================连接数据库=====================
17.Set Conn= Server.CreateObject("ADODB.Connection")
18.Conn.Connectionstring= "Driver={SQL Server};Server=192.168.1.3;UID=sa;PWD=;Database=zcmrs"
19.Con

n.open
20.'===================从表log中读取用户数据=====================
21.'定义RecordSet对象
22.Set rs=Server.CreateObject("ADODB.Recordset")
23.'设置Connection对象的ConnectionString
24.Set rs.ActiveConnection=Conn
25.'设置游标类型
26.rs.CursorType=3
27.'打开记录集
28.rs.Open "Select username,password from erpuser Where username='"&UserName&"'"
29.'===================身份验证======================
30.If rs.EOF Then
31.Errmsg="提示:用户不存在或密码错误"
32.Else
33.If UserPwd<>rs.Fields("password") Then
34.Errmsg="提示:登录失败!密码错误?"
35.Else '登录成功
36.Errmsg=""
37.Session("Passed")=True
38.Session("UserName")=rs.Fields("username")
39.'标识用户权限 Session("UserID")=rs.Fields("UserID")
40.End If
41.End If
42.End If
43.End If
44.'经过登录不成功,则画出登录表单
45.If Not Session("Passed")=True Then
46.%>
47.
48.
49.无标题文档
50.
55.
60.
61.
62.
63.


64.
65.

ERP系统登录

66.
" method="post" name="MyForm" id="MyForm">
67.

用户名:
68.


69.

密 码:
70.


71.

<%=Errmsg%>


72.


73.
74.
75.


76.

77.

78.
79.
80.
81.
82.<%
83.'

验证码:
84.'


85.response.End
86.End If
87.%>
要访问的页面erp.asp

1.
2.
3.
欢迎使用ERP查询系统,当前登录用户为:
4.<%
5.If Session("Passed")=True Then
6.Response.Write(Session("UserName"))
7.End If
8.%>退

出系统
9.


10.
以上每次打开erp.asp的时候,都首先执行Chkpwd.asp(),这样可以有效防止未授权用户访问指定网页.

logout.asp系统退出

1.
2.<%
3.Session("Passed")=false
4.Session("UserName")=""
5.Response.Redirect("index.asp")
6.%>
7.
使用图片提交表单

1.

2.
3.
4.
5.





三。简单的用户登录
ASP用户登录界面简单代码设计(2010-11-22 12:54:37)转载▼标签: 数据库用户登录界面代码设计源码杂谈 分类: 其他技巧
三个文件 一个html登陆界面 一个asp登陆后的界面 和一个mdb记录用户名密码的数据库文件

Tijiao3.html




请提供以下信息,然后单击提交。

姓名:

密码:





Fanhui3.asp





<%
n1=trim(request.form("myname"))
n2=trim(request.form("psw"))
if n1="" or n2="" then
response.write"

出错提示:用户名、密码不能为空!

"
response.write"

点击这里重新登录!

"
response.end
end if
set cn=server.createobject("adodb.recordset")
cn.open "select name,word from s1 where name='"& n1 &"' and word='"& n2 &"' ","driver={microsoft access driver (*.mdb)};dbq=" & Server.MapPath("s1.mdb")
if cn.eof then
response.write"

出错提示:用户名不存在或者用户名、密码不正确!

"
response.write"

点击这里重新登录!

"
else
response.write"

欢迎您成功登录本公司网站

"
response.write"

更改用户

"
cn.close
set cn=nothing
end if
%>



s1.mdb数据库文件,记录用户名和密码,表名为s1,字段名称分别为name和word

相关文档
最新文档