求助asp.net用户名密码验证码-验证修改

xuxkaaaa 2009-01-13 12:16:24
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title>复杂验证码</title>
<script type="text/javascript">
function ShowValidImage() {
var numkey = Math.random();
document.getElementById("imgRandom").src = "/ValidCode.aspx?NumKey="+numkey;
} </script>
<style type="text/css"> .style1
{ width: 408px; }
</style></head><body>
<form id="form1" runat="server">
<div> </div>
<asp:Panel ID="Panel1" runat="server" Height="131px">
<table class="style1" align="center"
style="border: thin solid #66CCFF; height: 131px;">
<tr> <td align="right" >
<asp:Label ID="UseN" runat="server" Text="用户名:"></asp:Label>
</td><td>
<asp:TextBox ID="Textuse" runat="server" Width="143px" Height="26px"></asp:TextBox>
</td></tr> <tr>
<td align="right" >
<asp:Label ID="UseP" runat="server" Text="密 码:"></asp:Label>
</td> <td>
<asp:TextBox ID="Textpwd" runat="server" Width="144px" Height="24px"></asp:TextBox>
</td></tr> <tr> <td align="right">
<asp:Label ID="UseY" runat="server" Text="验证码:"></asp:Label>
</td> <td>
<asp:TextBox ID="Textyzm" runat="server" Width="146px" Height="24px"></asp:TextBox>
<img id="imgRandom" alt="看不清?点击更换" onclick="ShowValidImage()" src="ValidCode.aspx" title="看不清?点击更换" /></td> </tr><tr>
<td colspan=2 align=center>
<asp:Button ID="LoginBtn" runat="server" onclick="LoginBtn_Click" Text="登 录" />
</td></tr> </table> </asp:Panel></form> </body></html>

ValidCode.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidCode.aspx.cs" Inherits="ComplexValide.ValidCode" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Untitled Page</title>
</head><body><form id="form1" runat="server">
<div></div></form></body></html>

ValidCode.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
namespace ComplexValide
{ public partial class ValidCode : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
Random rd = new Random();
string str = "ABCDEFGHIJKLMNOPQRSTURWXYZ0123456789复杂验证源码";
string my51aspx = "";
for (int i = 0; i < 6; i++)
{ my51aspx = my51aspx + str.Substring(rd.Next(42), 1); }
Session["Valid"] = my51aspx;
string bgFilePath = Server.MapPath(".\\images\\bg" + new Random().Next(5) + ".jpg");
System.Drawing.Image imgObj = System.Drawing.Image.FromFile(bgFilePath);
Bitmap newBitmap = new Bitmap(imgObj, 290, 80);
Graphics g = Graphics.FromImage(newBitmap);
SolidBrush brush = new SolidBrush(Color.Black);
String[] fontFamily ={ "Arial", "Verdana", "Comic Sans MS", "Impact", "Haettenschweiler", "Lucida Sans Unicode", "Garamond", "Courier New", "Book Antiqua", "Arial Narrow" };
for (int a = 0; a < my51aspx.Length; a++)
{ Font textFont = new Font(fontFamily[rd.Next(9)], 30, FontStyle.Bold);
g.DrawString(my51aspx.Substring(a, 1), textFont, brush, 40 + a * 36, 20); }
newBitmap.Save(Response.OutputStream, ImageFormat.Gif);
} } }

sql数据库aaa表userId(aa,bb,cc),pwd(11,22,33),RoleId(1,2,3)
用户输入正确的用户名、密码、验证码才能登录,且RoleId为1时登录a.aspx,为2时登录b.aspx,为3时登录c.aspx
请大家帮助修改代码!非常感谢!!!
...全文
424 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xtc_cn 2009-01-14
  • 打赏
  • 举报
回复
直接修改代码!!加注释!!
谢谢!!!
xtc_cn 2009-01-14
  • 打赏
  • 举报
回复
ValidCode.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidCode.aspx.cs" Inherits="ComplexValide.ValidCode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>


ValidCode.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;

//该源码下载自www.51aspx.com(51aspx.com)
namespace ComplexValide
{
public partial class ValidCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Random rd = new Random(); //创建随机数对象

//以下4行,产生由6个字母和数字组成的一个字符串
string str = "ABCDEFGHIJKLMNOPQRSTURWXYZ0123456789我的复杂验证源码";
string my51aspx = "";
for (int i = 0; i < 6; i++)
{
my51aspx = my51aspx + str.Substring(rd.Next(42), 1);
}
//验证码值存放到Session中用来比较
Session["Valid"] = my51aspx;

//以下三句,通过随机找一个现有图象产生一个画布Bitmap
string bgFilePath = Server.MapPath(".\\images\\bg" + new Random().Next(5) + ".jpg");//随机找个图象
System.Drawing.Image imgObj = System.Drawing.Image.FromFile(bgFilePath);
Bitmap newBitmap = new Bitmap(imgObj, 290, 80);//建立位图对象

Graphics g = Graphics.FromImage(newBitmap);//根据上面创建的位图对象创建绘图面
SolidBrush brush = new SolidBrush(Color.Black);//设置画笔颜色

//定义一个含10种字体的数组
String[] fontFamily ={ "Arial", "Verdana", "Comic Sans MS", "Impact", "Haettenschweiler", "Lucida Sans Unicode", "Garamond", "Courier New", "Book Antiqua", "Arial Narrow" };

//通过循环,绘制每个字符,
for (int a = 0; a < my51aspx.Length; a++)
{
Font textFont = new Font(fontFamily[rd.Next(9)], 30, FontStyle.Bold);//字体随机,字号大小30,加粗
//每次循环绘制一个字符,设置字体格式,画笔颜色,字符相对画布的X坐标,字符相对画布的Y坐标
g.DrawString(my51aspx.Substring(a, 1), textFont, brush, 40 + a * 36, 20);
}

//保存画的图片到输出流中
newBitmap.Save(Response.OutputStream, ImageFormat.Gif);

}

}
}

共四个文件!
ql数据库aaa表userId(aa,bb,cc),pwd(11,22,33),RoleId(1,2,3)
用户输入正确的用户名、密码、验证码才能登录,且RoleId为1时登录a.aspx,为2时登录b.aspx,为3时登录c.aspx
请大家帮助修改代码!非常感谢!!!
感谢楼上各位!!!!!!
xtc_cn 2009-01-14
  • 打赏
  • 举报
回复
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title>复杂验证码</title>
<script type="text/javascript">
function ShowValidImage() {
var numkey = Math.random();
document.getElementById("imgRandom").src = "/ValidCode.aspx?NumKey="+numkey;
} </script>
<style type="text/css"> .style1
{ width: 408px; }
</style> </head><body>
<form id="form1" runat="server">
<div> </div>
<asp:Panel ID="Panel1" runat="server" Height="131px">
<table class="style1" align="center"
style="border: thin solid #66CCFF; height: 131px;">
<tr> <td align="right" >
<asp:Label ID="UseN" runat="server" Text="用户名:"></asp:Label>
</td><td>
<asp:TextBox ID="Textuse" runat="server" Width="143px" Height="26px"></asp:TextBox>
</td></tr> <tr>
<td align="right" >
<asp:Label ID="UseP" runat="server" Text="密 码:"></asp:Label>
</td> <td>
<asp:TextBox ID="Textpwd" runat="server" Width="144px" Height="24px"></asp:TextBox>
</td></tr> <tr> <td align="right">
<asp:Label ID="UseY" runat="server" Text="验证码:"></asp:Label>
</td> <td>
<asp:TextBox ID="Textyzm" runat="server" Width="146px" Height="24px"></asp:TextBox>
<img id="imgRandom" alt="看不清?点击更换" onclick="ShowValidImage()" src="ValidCode.aspx" title="看不清?点击更换" /></td> </tr><tr>
<td colspan=2 align=center>
<asp:Button ID="LoginBtn" runat="server" onclick="LoginBtn_Click" Text="登 录" />
</td></tr> </table> </asp:Panel></form> </body></html>


Default.aspx.cs
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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void LoginBtn_Click(object sender, EventArgs e)
{

}
}
guxiaobei 2009-01-14
  • 打赏
  • 举报
回复
用户名和密码是从数据库里面取出数据验证就不说了。。
校验码是另外写的。。。
下面是一个随机生成四个校验码的代码(数字或者是大写字母)。
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.Drawing.Imaging;
using System.IO;

public partial class image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.GenImg(this.GenCode(4));
//将验证码存储到session中以便需要时进行验证
Session["image"] = this.GenCode(4);
}
//任意产生4个验证码数组
private String GenCode(int num)
{
string [] source={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
string code="";
Random rd=new Random();
for(int i=0;i<num;i++)
{
code+=source[rd.Next(0,source.Length)];
}
return code;
}
//生成图片
private void GenImg(string code)
{
//定义画板
Bitmap mypalette=new Bitmap(60,20);
//在画板上定义绘图的实例
Graphics gh=Graphics.FromImage(mypalette);
//定义一个矩形
Rectangle rc = new Rectangle(0,0,60,20);
//填充矩形
gh.FillRectangle(new SolidBrush(Color.Blue),rc);
//在矩形内画出字符串
gh.DrawString(code,new Font("宋体",16),new SolidBrush(Color.White),rc);
//将图片显示出来
mypalette.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
gh.Dispose();
mypalette.Dispose();
}
}
che2piaopiao 2009-01-13
  • 打赏
  • 举报
回复
mark
ReyZhang 2009-01-13
  • 打赏
  • 举报
回复
up,如1楼
wanghao111 2009-01-13
  • 打赏
  • 举报
回复
mark
Teng_s2000 2009-01-13
  • 打赏
  • 举报
回复
role 和user两个表如何关联的啊,可以进行判断

txtUserName,txtPassword,txtRole与数据库比较

ValidateCode与Session比较

根据条件进行跳转

62,269

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧